Passed
Push — master ( 7cd6c5...dd963e )
by Laurens
04:05
created

CompanyFactory::extractAddresses()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Werkspot\KvkApi\Client\Factory\Profile;
6
7
use DateTime;
8
use Werkspot\KvkApi\Client\Factory\AbstractFactory;
9
use Werkspot\KvkApi\Client\Factory\Profile\Company\AddressFactoryInterface;
10
use Werkspot\KvkApi\Client\Factory\Profile\Company\BusinessActivityFactoryInterface;
11
use Werkspot\KvkApi\Client\Factory\Profile\Company\TradeNamesFactoryInterface;
12
use Werkspot\KvkApi\Client\Profile\Company;
13
14
final class CompanyFactory extends AbstractFactory implements CompanyFactoryInterface
15
{
16
    /**
17
     * @var AddressFactoryInterface
18
     */
19
    private $addressFactory;
20
21
    /**
22
     * @var BusinessActivityFactoryInterface
23
     */
24
    private $businessActivityFactory;
25
26
    /**
27
     * @var TradeNamesFactoryInterface
28
     */
29
    private $tradeNamesFactory;
30
31 19
    public function __construct(
32
        TradeNamesFactoryInterface $tradeNamesFactory,
33
        BusinessActivityFactoryInterface $businessActivityFactory,
34
        AddressFactoryInterface $addressFactory
35
    ) {
36 19
        $this->tradeNamesFactory = $tradeNamesFactory;
37 19
        $this->businessActivityFactory = $businessActivityFactory;
38 19
        $this->addressFactory = $addressFactory;
39 19
    }
40
41 17
    public function fromArray(array $data): Company
42
    {
43 17
        return new Company(
44 17
            $this->extractIntegerOrNull('kvkNumber', $data),
45 17
            $this->extractStringOrNull('branchNumber', $data),
46 17
            $this->extractIntegerOrNull('rsin', $data),
47 17
            $this->tradeNamesFactory->fromArray($data['tradeNames']),
48 17
            $this->extractStringOrNull('legalForm', $data),
49 17
            $this->extractBusinessActivities($data),
50 17
            $this->extractBoolean('hasEntryInBusinessRegister', $data),
51 17
            $this->extractBoolean('hasCommercialActivities', $data),
52 17
            $this->extractBoolean('hasNonMailingIndication', $data),
53 17
            $this->extractBoolean('isLegalPerson', $data),
54 17
            $this->extractBoolean('isBranch', $data),
55 17
            $this->extractBoolean('isMainBranch', $data),
56 17
            $this->extractIntegerOrNull('employees', $data),
57 17
            new DateTime($data['foundationDate']),
58 17
            new DateTime($data['registrationDate']),
59 17
            $this->extractAddresses($data)
60
        );
61
    }
62
63 17
    private function extractBusinessActivities($data): ?array
64
    {
65 17
        if (array_key_exists('businessActivities', $data)) {
66 15
            return $this->businessActivityFactory->fromArray($data['businessActivities']);
67
        }
68
69 2
        return null;
70
    }
71
72 17
    private function extractAddresses($data): ?array
73
    {
74 17
        if (array_key_exists('addresses', $data)) {
75 16
            return $this->addressFactory->fromArray($data['addresses']);
76
        }
77
78 1
        return null;
79
    }
80
}
81