Completed
Push — api ( cecea0...116a36 )
by Kamil
29:57 queued 30s
created

ApiPlatformSecurityClient   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 56
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A prepareLoginRequest() 0 5 1
A setEmail() 0 4 1
A setPassword() 0 4 1
A call() 0 11 1
A isLoggedIn() 0 9 2
A getErrorMessage() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\Behat\Client;
6
7
use Symfony\Component\BrowserKit\AbstractBrowser;
8
use Symfony\Component\HttpFoundation\Response;
9
10
final class ApiPlatformSecurityClient implements ApiSecurityClientInterface
11
{
12
    /** @var AbstractBrowser */
13
    private $client;
14
15
    /** @var array */
16
    private $request = [];
17
18
    public function __construct(AbstractBrowser $client)
19
    {
20
        $this->client = $client;
21
    }
22
23
    public function prepareLoginRequest(): void
24
    {
25
        $this->request['url'] = '/new-api/authentication-token';
26
        $this->request['method'] = 'POST';
27
    }
28
29
    public function setEmail(string $email): void
30
    {
31
        $this->request['body']['email'] = $email;
32
    }
33
34
    public function setPassword(string $password): void
35
    {
36
        $this->request['body']['password'] = $password;
37
    }
38
39
    public function call(): void
40
    {
41
        $this->client->request(
42
            $this->request['method'],
43
            $this->request['url'],
44
            [],
45
            [],
46
            ['CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'],
47
            json_encode($this->request['body'])
48
        );
49
    }
50
51
    public function isLoggedIn(): bool
52
    {
53
        $response = $this->client->getResponse();
54
55
        return
56
            isset(json_decode($response->getContent(), true)['token']) &&
57
            $response->getStatusCode() !== Response::HTTP_UNAUTHORIZED
58
        ;
59
    }
60
61
    public function getErrorMessage(): string
62
    {
63
        return json_decode($this->client->getResponse()->getContent(), true)['message'];
64
    }
65
}
66