Completed
Pull Request — master (#1596)
by Thomas
02:56
created

DivisionFactory::build()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 15
cts 15
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 4
nop 4
crap 4
1
<?php
2
/**
3
 * This file is part of the browscap package.
4
 *
5
 * Copyright (c) 1998-2017, Browser Capabilities Project
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types = 1);
12
namespace Browscap\Data\Factory;
13
14
use Browscap\Data\Division;
15
use Browscap\Data\Validator\DivisionData;
16
use Psr\Log\LoggerInterface;
17
18
/**
19
 * Class DivisionFactory
20
 *
21
 * @category   Browscap
22
 *
23
 * @author     Thomas Müller <[email protected]>
24
 */
25
class DivisionFactory
26
{
27
    /**
28
     * @var \Psr\Log\LoggerInterface
29
     */
30
    private $logger;
31
32
    /**
33
     * @var UseragentFactory
34
     */
35
    private $useragentFactory;
36
37
    /**
38
     * @var \Browscap\Data\Validator\DivisionData
39
     */
40
    private $divisionData;
41
42
    /**
43
     * @param \Psr\Log\LoggerInterface $logger
44
     */
45 30
    public function __construct(LoggerInterface $logger)
46
    {
47 30
        $this->logger           = $logger;
48 30
        $this->useragentFactory = new UseragentFactory();
49 30
        $this->divisionData     = new DivisionData();
50 30
    }
51
52
    /**
53
     * @param array  $divisionData
54
     * @param string $filename
55
     * @param array  &$allDivisions
56
     * @param bool   $isCore
57
     *
58
     * @throws \UnexpectedValueException If required attibutes are missing in the division
59
     * @throws \LogicException
60
     *
61
     * @return \Browscap\Data\Division
62
     */
63 7
    public function build(
64
        array $divisionData,
65
        string $filename,
66
        array &$allDivisions,
67
        bool $isCore
68
    ) : Division {
69 7
        $this->divisionData->validate($divisionData, $filename);
70
71 7
        if (isset($divisionData['versions']) && is_array($divisionData['versions'])) {
72 1
            $versions = $divisionData['versions'];
73
        } else {
74 6
            $versions = ['0.0'];
75
        }
76
77 7
        if (1 < count($divisionData['userAgents'])) {
78 2
            $this->logger->info('division "' . $divisionData['division'] . '" has more than one "userAgents" section, try to separate them');
79
        }
80
81 7
        return new Division(
82 7
            $divisionData['division'],
83 7
            (int) $divisionData['sortIndex'],
84 7
            $this->useragentFactory->build($divisionData['userAgents'], $versions, $isCore, $allDivisions, $filename),
85 7
            (bool) $divisionData['lite'],
86 7
            (bool) $divisionData['standard'],
87 7
            $versions,
88 7
            mb_substr($filename, (int) mb_strpos($filename, 'resources/'))
89
        );
90
    }
91
}
92