Passed
Push — master ( ec1f20...2f2459 )
by Ross
02:33
created

SubDomainInfoFactory::getKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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