PaymentType::favorite()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoman4eg\Nalog\Api;
5
6
use Psr\Http\Client\ClientExceptionInterface;
7
use Shoman4eg\Nalog\ErrorHandler;
8
use Shoman4eg\Nalog\Exception;
9
use Shoman4eg\Nalog\Model\PaymentType\PaymentType as PaymentTypeModel;
10
use Shoman4eg\Nalog\Model\PaymentType\PaymentTypeCollection;
11
12
/**
13
 * @author Artem Dubinin <[email protected]>
14
 */
15
final class PaymentType extends BaseHttpApi
16
{
17
    /**
18
     * @throws ClientExceptionInterface
19
     * @throws Exception\DomainException
20
     */
21
    public function table(): PaymentTypeCollection
22
    {
23
        $response = $this->httpGet('/payment-type/table');
24
25
        if ($response->getStatusCode() >= 400) {
26
            (new ErrorHandler())->handleResponse($response);
27
        }
28
29
        return $this->hydrator->hydrate($response, PaymentTypeCollection::class);
30
    }
31
32
    /**
33
     * @throws ClientExceptionInterface
34
     * @throws Exception\DomainException
35
     */
36
    public function favorite(): ?PaymentTypeModel
37
    {
38
        $paymentTypes = $this->table();
39
40
        foreach ($paymentTypes as $paymentType) {
41
            /** @var PaymentTypeModel $paymentType */
42
            if ($paymentType->isFavorite()) {
43
                return $paymentType;
44
            }
45
        }
46
47
        return null;
48
    }
49
}
50