Passed
Branch master (5e8728)
by Oss
02:18
created

CartsGuru::track()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @category    Brownie/CartsGuru
4
 * @author      Brownie <[email protected]>
5
 * @license     http://www.gnu.org/copyleft/lesser.html
6
 */
7
8
namespace Brownie\CartsGuru;
9
10
use Brownie\CartsGuru\HTTPClient\HTTPClient;
11
use Brownie\CartsGuru\Model\Cart;
12
use Brownie\CartsGuru\Model\Order;
13
use Brownie\CartsGuru\Model\DataModel;
14
15
/**
16
 * CartsGuru API.
17
 */
18
class CartsGuru
19
{
20
21
    /**
22
     * @var HTTPClient
23
     */
24
    private $httpClient;
25
26
    /**
27
     * Site ID.
28
     *
29
     * @var string
30
     */
31
    private $siteId;
32
33
    /**
34
     * Sets incoming data.
35
     *
36
     * @param HTTPClient    $httpClient     HTTP client.
37
     * @param string        $siteId         Site ID.
38
     */
39 2
    public function __construct(HTTPClient $httpClient, $siteId)
40
    {
41
        $this
42 2
            ->setHttpClient($httpClient)
43 2
            ->setSiteId($siteId);
44 2
    }
45
46
    /**
47
     * Sets an HTTP client.
48
     * Returns the current object.
49
     *
50
     * @param HTTPClient $httpClient Http client
51
     *
52
     * @return self
53
     */
54 2
    private function setHttpClient(HTTPClient $httpClient)
55
    {
56 2
        $this->httpClient = $httpClient;
57 2
        return $this;
58
    }
59
60
    /**
61
     * Returns HTTP client.
62
     *
63
     * @return HTTPClient
64
     */
65 2
    private function getHttpClient()
66
    {
67 2
        return $this->httpClient;
68
    }
69
70
    /**
71
     * Set site ID.
72
     *
73
     * @param string    $siteId     Site ID
74
     *
75
     * @return self
76
     */
77 2
    private function setSiteId($siteId)
78
    {
79 2
        $this->siteId = $siteId;
80 2
        return $this;
81
    }
82
83
    /**
84
     * Return site ID.
85
     *
86
     * @return string
87
     */
88 2
    private function getSiteId()
89
    {
90 2
        return $this->siteId;
91
    }
92
93
    /**
94
     * Track Cart.
95
     * Returns the status of the request.
96
     *
97
     * @param Cart      $cart    Description of the cart.
98
     *
99
     * @return boolean
100
     */
101 1
    public function trackCart(Cart $cart)
102
    {
103 1
        return $this->track($cart);
104
    }
105
106
    /**
107
     * Track Order.
108
     * Returns the status of the request.
109
     *
110
     * @param Order     $order      Description of the order.
111
     *
112
     * @return boolean
113
     */
114 1
    public function trackOrder(Order $order)
115
    {
116 1
        return $this->track($order);
117
    }
118
119
    /**
120
     * Request track.
121
     * Returns the status of the request.
122
     *
123
     * @param DataModel     $model      Description of the data.
124
     *
125
     * @return bool
126
     */
127 2
    private function track(DataModel $model)
128
    {
129 2
        $model->setSiteId($this->getSiteId());
0 ignored issues
show
Bug introduced by
The method setSiteId() does not exist on Brownie\CartsGuru\Model\DataModel. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
        $model->/** @scrutinizer ignore-call */ setSiteId($this->getSiteId());
Loading history...
130 2
        $model->validate();
131
132
        $response = $this
133 2
            ->getHttpClient()
134 2
            ->request(
135 2
                HTTPClient::HTTP_CODE_200,
136 2
                $model->getEndpoint(),
137 2
                $model,
138 2
                HTTPClient::HTTP_METHOD_POST
139
            );
140
141 2
        return isset($response['response']['status']) && ('success' == $response['response']['status']);
142
    }
143
}
144