Completed
Pull Request — master (#1596)
by Thomas
03:22
created

DivisionFactory::build()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 0
cts 15
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 4
nop 4
crap 20
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
     * @param \Psr\Log\LoggerInterface $logger
39
     */
40
    public function __construct(LoggerInterface $logger)
41
    {
42
        $this->logger           = $logger;
43
        $this->useragentFactory = new UseragentFactory();
44
    }
45
46
    /**
47
     * @param array  $divisionData
48
     * @param string $filename
49
     * @param array  &$allDivisions
50
     * @param bool   $isCore
51
     *
52
     * @throws \UnexpectedValueException If required attibutes are missing in the division
53
     * @throws \LogicException
54
     *
55
     * @return \Browscap\Data\Division
56
     */
57
    public function build(
58
        array $divisionData,
59
        string $filename,
60
        array &$allDivisions,
61
        bool $isCore
62
    ) : Division {
63
        DivisionData::validate($divisionData, $filename);
64
65
        if (isset($divisionData['versions']) && is_array($divisionData['versions'])) {
66
            $versions = $divisionData['versions'];
67
        } else {
68
            $versions = ['0.0'];
69
        }
70
71
        if (1 < count($divisionData['userAgents'])) {
72
            $this->logger->info('division "' . $divisionData['division'] . '" has more than one "userAgents" section, try to separate them');
73
        }
74
75
        return new Division(
76
            $divisionData['division'],
77
            (int) $divisionData['sortIndex'],
78
            $this->useragentFactory->build($divisionData['userAgents'], $versions, $isCore, $allDivisions, $filename),
79
            (bool) $divisionData['lite'],
80
            (bool) $divisionData['standard'],
81
            $versions,
82
            mb_substr($filename, (int) mb_strpos($filename, 'resources/'))
83
        );
84
    }
85
}
86