Emarsys::initialize()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 9
cp 0.8889
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 0
crap 4.0218
1
<?php
2
3
/*
4
 * A php library for using the Emarsys API.
5
 *
6
 * @link      https://github.com/quitoque/emarsys-php-client
7
 * @package   emarsys-php-client
8
 * @license   MIT
9
 * @copyright Copyright (c) 2017 Quitoque <[email protected]>
10
 */
11
12
namespace Emarsys;
13
14
use Emarsys\Api\ApiInterface;
15
use Emarsys\Exception\Api\LogicException;
16
use Emarsys\Exception\Api\NotFoundException;
17
use Emarsys\HttpClient\Builder;
18
use Http\Client\Common\PluginClient;
19
use Http\Message\MessageFactory;
20
21
/**
22
 * Class Emarsys.
23
 *
24
 * @author Claude Khedhiri <[email protected]>
25
 *
26
 * @method Api\Contacts contacts()
27
 * @method Api\Fields fields()
28
 */
29
class Emarsys
30
{
31
    /**
32
     * @var PluginClient
33
     */
34
    private $httpClient;
35
36
    /**
37
     * @var Builder
38
     */
39
    private $httpBuilder;
40
41
    /**
42
     * @var MessageFactory
43
     */
44
    private $requestFactory;
45
46 6
    public function __construct($username, $secret)
0 ignored issues
show
Unused Code introduced by
The parameter $secret is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
    {
48 6
        $this->parameters = array(
0 ignored issues
show
Bug introduced by
The property parameters does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49 6
            'username' => $username,
50 6
            'secret' => $username,
51
        );
52 6
    }
53
54 3
    public function __call($name, $arguments)
55
    {
56 3
        $class = sprintf('\Emarsys\Api\%s', ucfirst($name));
57
58 3
        if (false === class_exists($class)) {
59 1
            throw  new NotFoundException($class);
60
        }
61
62 2
        if ($class instanceof ApiInterface) {
63
            throw new LogicException(sprintf('%s must be extend Emarsys\Api\ApiInterface'));
64
        }
65
66 2
        $this->initialize();
67
68 2
        $class = new $class(
69 2
            $this->httpClient,
70 2
            $this->requestFactory
71
        );
72
73 2
        return $class;
74
    }
75
76
    /**
77
     * @param $httpBuilder
78
     */
79 2
    public function setHttpBuilder($httpBuilder)
80
    {
81 2
        $this->httpBuilder = $httpBuilder;
82 2
    }
83
84 2
    private function initialize()
85
    {
86 2
        if (null !== $this->httpClient && null !== $this->requestFactory) {
87
            return;
88
        }
89
90 2
        if (null === $this->httpBuilder) {
91 2
            $this->httpBuilder = new Builder();
92
        }
93
94 2
        $this->httpBuilder->setParameters($this->parameters);
95
96 2
        $this->httpClient = $this->httpBuilder->getHttpClient();
97 2
        $this->requestFactory = $this->httpBuilder->getRequestFactory();
98 2
    }
99
}
100