AuthenticationService::getHash()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 16
rs 9.9666
1
<?php
2
3
/*
4
 * This file is part of PHP CS Fixer.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *     Dariusz Rumiński <[email protected]>
8
 *
9
 * This source file is subject to the MIT license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace Etrias\EwarehousingConnector\Services;
14
15
use Etrias\EwarehousingConnector\Client\EwarehousingClient;
16
use Etrias\EwarehousingConnector\Response\GetContextResponse;
17
use Etrias\EwarehousingConnector\Serializer\ServiceTrait;
18
use JMS\Serializer\SerializerInterface;
19
use Symfony\Component\Cache\Adapter\AdapterInterface;
20
use Symfony\Component\Cache\Adapter\NullAdapter;
21
22
class AuthenticationService implements AuthenticationServiceInterface
23
{
24
    use ServiceTrait;
25
26
    const CACHE_KEY = 'eWh-context-cc503444-6c3a-4fb7-b492-a75f2caa63a9';
27
    const CACHE_TTL = 60 * 15;
28
29
    /**
30
     * @var EwarehousingClient
31
     */
32
    protected $client;
33
34
    /** @var AdapterInterface */
35
    protected $cacheAdapter;
36
37
    /**
38
     * @var string
39
     */
40
    private $userName;
41
    /**
42
     * @var string
43
     */
44
    private $customerId;
45
    /**
46
     * @var string
47
     */
48
    private $password;
49
50
    /** @var bool */
51
    private $passwordIsPlain;
52
53
    /** @var  integer */
54
    protected $cacheTtl;
55
56
    /** @var  string */
57
    protected $cacheKey;
58
59
    /**
60
     * AuthenticationService constructor.
61
     *
62
     * @param EwarehousingClient    $client
63
     * @param SerializerInterface   $serializer
64
     * @param string                $userName
65
     * @param string                $customerId
66
     * @param string                $password
67
     * @param AdapterInterface|null $cacheAdapter
68
     * @param bool                  $passwordIsPlain
69
     */
70
    public function __construct(
71
        EwarehousingClient $client,
72
        SerializerInterface $serializer,
73
        $userName,
74
        $customerId,
75
        $password,
76
        AdapterInterface $cacheAdapter = null,
77
        $passwordIsPlain = true
78
    ) {
79
        $this->userName = $userName;
80
        $this->customerId = $customerId;
81
        $this->password = $password;
82
        $this->client = $client;
83
        if ($cacheAdapter === null) {
84
            $this->cacheAdapter = new NullAdapter();
85
        } else {
86
            $this->cacheAdapter = $cacheAdapter;
87
        }
88
        $this->serializer = $serializer;
89
        $this->passwordIsPlain = $passwordIsPlain;
90
        $this->cacheTtl = self::CACHE_TTL;
91
        $this->cacheKey = self::CACHE_KEY;
92
    }
93
94
    /**
95
     * @return int
96
     */
97
    public function getCacheTtl()
98
    {
99
        return $this->cacheTtl;
100
    }
101
102
    /**
103
     * @param int $cacheTtl
104
     * @return AuthenticationService
105
     */
106
    public function setCacheTtl($cacheTtl)
107
    {
108
        $this->cacheTtl = $cacheTtl;
109
110
        return $this;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getCacheKey()
117
    {
118
        return $this->cacheKey;
119
    }
120
121
    /**
122
     * @param string $cacheKey
123
     * @return AuthenticationService
124
     */
125
    public function setCacheKey($cacheKey)
126
    {
127
        $this->cacheKey = $cacheKey;
128
129
        return $this;
130
    }
131
132
    /**
133
     * @return GetContextResponse
134
     */
135
    public function getContext()
136
    {
137
        $data = [
138
            'username' => $this->userName,
139
            'customer_id' => $this->customerId,
140
            'password' => $this->passwordIsPlain ? md5($this->password) : $this->password,
141
        ];
142
143
        $guzzleResponse = $this->client->post('2/auth', ['form_params' => $data]);
144
145
        return $this->deserializeResponse($guzzleResponse, GetContextResponse::class);
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function getHash()
152
    {
153
        $cacheItem = $this->cacheAdapter->getItem($this->getCacheKey());
154
        if ($cacheItem->isHit()) {
155
            return $cacheItem->get();
156
        }
157
158
        $context = $this->getContext();
159
160
        $cacheItem
161
            ->expiresAfter($this->getCacheTtl())
162
            ->set($context->getContext());
163
164
        $this->cacheAdapter->save($cacheItem);
165
166
        return $context->getContext();
167
    }
168
169
    /**
170
     * @return string
171
     */
172
    public function getUserName()
173
    {
174
        return $this->userName;
175
    }
176
}
177