ScaleEngineCommandFactory::factory()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
namespace FloSports\ScaleEngine\CommandFactory;
3
4
use FloSports\ScaleEngine\Visitor\ScaleEngineRequestVisitor;
5
use Guzzle\Service\Command\Factory\ServiceDescriptionFactory;
6
use Guzzle\Service\Description\ServiceDescriptionInterface;
7
use Guzzle\Inflection\InflectorInterface;
8
9
/**
10
 * A Guzzle command factory that extends commands to work against the
11
 * ScaleEngine API.
12
 */
13
class ScaleEngineCommandFactory extends ServiceDescriptionFactory
14
{
15
    /** @type string The API secret used to sign requests. */
16
    private $_apiSecret;
17
18
    /**
19
     * Create the command factory for the ScaleEngine API requests.
20
     *
21
     * @param string $apiSecret The API secret used to sign requests.
22
     * @param ServiceDescriptionInterface $description Service description.
23
     * @param InflectorInterface $inflector Optional inflector to use if the
24
     *     command is not at first found.
25
     */
26
    public function __construct(
27
        $apiSecret,
28
        ServiceDescriptionInterface $description,
29
        InflectorInterface $inflector = null
30
    ) {
31
        parent::__construct($description, $inflector);
32
        $this->_apiSecret = $apiSecret;
33
    }
34
35
    /**
36
     * Create a Guzzle command using the applied service description.
37
     *
38
     * Extends the command created by the default ServiceDescriptionFactory
39
     * behavior with a request visitor that signs and encodes requests to
40
     * ScaleEngine's API.
41
     *
42
     * @see \FloSports\ScaleEngine\Visitor\ScaleEngineRequestVisitor
43
     * @param string $name The name of the command to create.
44
     * @param array $args The parameters to the command.
45
     * @return \Guzzle\Service\Command\CommandInterface|null The constructed
46
     *     command.
47
     */
48
    public function factory($name, array $args = [])
49
    {
50
        $command = parent::factory($name, $args);
51
        if (!$command) {
52
            return null;
53
        }
54
55
        $command->getRequestSerializer()->addVisitor(
56
            'scaleengineParameter',
57
            new ScaleEngineRequestVisitor($this->_apiSecret)
58
        );
59
60
        return $command;
61
    }
62
}
63