Package::setPackagist()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * MIT License
5
 *
6
 * Copyright (c) 2016 Bernardo Secades
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in all
16
 * copies or substantial portions of the Software.
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
namespace BernardoSecades\Packagist\SecurityChecker\ValueObject;
27
28
use BernardoSecades\Packagist\SecurityChecker\Exception\Packagist\NotFollowSemanticVersioningException;
29
use Doctrine\Common\Inflector\Inflector;
30
31
/**
32
 * @author bernardosecades <[email protected]>
33
 */
34
class Package
35
{
36
    /** @var  string */
37
    protected $name;
38
39
    /** @var  string */
40
    protected $description;
41
42
    /** @var  string */
43
    protected $version;
44
45
    /** @var  string */
46
    protected $versionNormalized;
47
48
    /** @var array  */
49
    protected $source = [];
50
51
    /** @var  string */
52
    protected $type;
53
54
    /** @var  boolean */
55
    protected $packagist = false;
56
57
    /** @var  boolean */
58
    protected $semanticVersioning = false;
59
60
    /** @var  boolean */
61
    protected $bug = false;
62
63
    /**
64
     * @return boolean
65
     */
66 7
    public function hasBug()
67
    {
68 7
        return $this->bug;
69
    }
70
71
    /**
72
     * @return boolean
73
     */
74 5
    public function hasPackagist()
75
    {
76 5
        return $this->packagist;
77
    }
78
79
    /**
80
     * @return string
81
     */
82 8
    public function getName()
83
    {
84 8
        return $this->name;
85
    }
86
87
    /**
88
     * @return string
89
     */
90 1
    public function getType()
91
    {
92 1
        return $this->type;
93
    }
94
95
    /**
96
     * @return string
97
     */
98 1
    public function getVersionNormalized()
99
    {
100 1
        return $this->versionNormalized;
101
    }
102
103
    /**
104
     * @return string
105
     */
106 9
    public function getVersion()
107
    {
108 9
        return $this->version;
109
    }
110
111
    /**
112
     * @return string
113
     */
114 1
    public function getDescription()
115
    {
116 1
        return $this->description;
117
    }
118
119
    /**
120
     * @return string
121
     */
122 4
    public function getUrl()
123
    {
124 4
        return isset($this->source['url']) ? $this->source['url'] : '';
125
    }
126
127
    /**
128
     * @param array $data
129
     */
130 8
    public function fromArray(array $data)
131
    {
132 8
        foreach ($data as $key => $value) {
133 8
            $property = Inflector::camelize($key);
134 8
            if (property_exists($this, $property)) {
135 8
                $this->$property = $value;
136 8
            }
137 8
        }
138 8
    }
139
140
    /**
141
     * @return bool
142
     */
143 9
    public function supportSemanticVersioning()
144
    {
145 9
        return 3 === count(explode('.', $this->getVersion()));
146
    }
147
148
    /**
149
     * @return string
150
     */
151 8
    public function getVersionWithNextPatchVersion()
152
    {
153 8
        return sprintf('%s.%s.%s', $this->getMajorVersion(), $this->getMinorVersion(), $this->getNextPatchVersion());
154
    }
155
156
    /**
157
     * @param bool $value
158
     */
159 6
    public function setSemanticVersioning($value)
160
    {
161 6
        $this->semanticVersioning = (bool) $value;
162 6
    }
163
164
    /**
165
     * @param bool $hasPackagist
166
     */
167 6
    public function setPackagist($hasPackagist)
168
    {
169 6
        $this->packagist = (bool) $hasPackagist;
170 6
    }
171
172
    /**
173
     * @param bool $hasBug
174
     */
175 3
    public function setBug($hasBug)
176
    {
177 3
        $this->bug = (bool) $hasBug;
178 3
    }
179
180
    /**
181
     * @return string
182
     */
183 8
    protected function getMajorVersion()
184
    {
185 8
        list($majorVersion, , ) = $this->getSemanticVersioning();
186
187 7
        return $majorVersion;
188
    }
189
190
    /**
191
     * @return string
192
     */
193 7
    protected function getMinorVersion()
194
    {
195 7
        list(, $minorVersion, ) = $this->getSemanticVersioning();
196
197 7
        return $minorVersion;
198
    }
199
200
    /**
201
     * @return string
202
     */
203 7
    protected function getPatchVersion()
204
    {
205 7
        list(, , $patchVersion) = $this->getSemanticVersioning();
206
207 7
        return $patchVersion;
208
    }
209
210
    /**
211
     * @return array Example: [major, minor, patch]
212
     */
213 8
    protected function getSemanticVersioning()
214
    {
215 8
        if (!$this->supportSemanticVersioning()) {
216 4
            throw new NotFollowSemanticVersioningException(sprintf('Package %s not follow semantic version', $this->getName()));
217
        }
218
219 7
        return explode('.', $this->getVersion());
220
    }
221
222
    /**
223
     * @return string
224
     */
225 7
    protected function getNextPatchVersion()
226
    {
227 7
        return (string) ((int) $this->getPatchVersion()+1);
228
    }
229
}
230