Skip to content

Create factory methods in all API classes #368

@Art4

Description

@Art4

Declaring the classes in the Redmine\Api\ namespace as final will allow us to make more future changes without worrying to much about BC.

To achieve this in a FC way we could create new factory methods create(HttpClient $client): self in all API child classes.

class ProjectApi extends AbstractApi
{
    final public static function fromClient(HttpClient $client): self
    {
        return new self($client);
    }

    /**
     * @param Client|HttpClient $client
     */
    public function __construct($client)
    {
        if (get_called_class() !== __CLASS__) {
            @trigger_error('Extending ' . __CLASS__ . ' is deprecated since v2.x, it will become final in v3.0.0.');
        }

        if ($client instanceof Client) {
            $this->client = $client;
        }

        // Handle HttpClient...
    }
}

For the future: Using the HttpClient in the API classes makes the AbstractApi nearly obsolete on the long run and can be deprecated. Extending it should also trigger a deprecation.

abstract class AbstractApi 
{
    /**
     * @param Client|HttpClient $client
     */
    public function __construct($client)
    {
        @trigger_error(__CLASS__ . ' is deprecated since v2.x and will be removed in v3.0.0.');

        // old code...
    }
}

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions