SubDomainInfoFactory::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
declare(strict_types = 1);
3
/**
4
 *
5
 * Copyright (C) 2018  Ross Mitchell
6
 *
7
 * This file is part of RossMitchell/UpdateCloudFlare.
8
 *
9
 * RossMitchell/UpdateCloudFlare is free software: you can redistribute
10
 * it and/or modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
namespace RossMitchell\UpdateCloudFlare\Factories\Data;
24
25
use RossMitchell\UpdateCloudFlare\Data\IpType;
26
use RossMitchell\UpdateCloudFlare\Data\SubDomainInfo;
27
use Symfony\Component\Console\Exception\LogicException;
28
29
/**
30
 * Class SubDomainInfoFactory
31
 * @package RossMitchell\UpdateCloudFlare\Factories\Data
32
 */
33
class SubDomainInfoFactory
34
{
35
    /**
36
     * @var SubDomainInfo[]
37
     */
38
    private $created = [];
39
40
    /**
41
     * @param string $subDomain
42
     * @param string $type
43
     *
44
     * @return SubDomainInfo
45
     * @throws LogicException
46
     */
47 31
    public function create(string $subDomain, string $type): SubDomainInfo
48
    {
49 31
        $ipType              = new IpType($type);
50 31
        $info                = new SubDomainInfo($subDomain, $ipType);
51 31
        $key                 = $this->getKey($subDomain, $type);
52 31
        $this->created[$key] = $info;
53
54 31
        return $info;
55
    }
56
57
    /**
58
     * @param string $subDomain
59
     * @param string $type
60
     *
61
     * @return SubDomainInfo
62
     * @throws LogicException
63
     */
64 13
    public function get(string $subDomain, string $type): SubDomainInfo
65
    {
66 13
        $key = $this->getKey($subDomain, $type);
67 13
        if (!isset($this->created[$key])) {
68 12
            $this->create($subDomain, $type);
69
        }
70
71 13
        return $this->created[$key];
72
    }
73
74
    /**
75
     * @param string $subDomain
76
     * @param string $type
77
     *
78
     * @return string
79
     */
80
    private function getKey(string $subDomain, string $type): string
81
    {
82
        return "${subDomain}_${type}";
83
    }
84
}
85