Version   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 118
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A set() 0 15 4
A asString() 0 4 1
A getMajor() 0 4 1
A getMinor() 0 4 1
A getPatch() 0 4 1
A setDate() 0 4 1
A getDate() 0 4 1
1
<?php
2
3
namespace kujaff\VersionsBundle\Entity;
4
5
use kujaff\VersionsBundle\Exception\VersionException;
6
7
/**
8
 * Information about a version
9
 */
10
class Version
11
{
12
    /**
13
     * @var int
14
     */
15
    private $major;
16
17
    /**
18
     * @var int
19
     */
20
    private $minor;
21
22
    /**
23
     * @var int
24
     */
25
    private $patch;
26
27
    /**
28
     * @var \DateTime
29
     */
30
    private $date;
31
32
    /**
33
     * Constructor
34
     *
35
     * @param string $version Full version (ex : 1.2.3)
36
     * @param \DateTime $date
37
     */
38
    public function __construct($version = null, \DateTime $date = null)
39
    {
40
        if ($version !== null) {
41
            $this->set($version);
42
        }
43
        $this->setDate($date);
44
    }
45
46
    /**
47
     * Define version
48
     *
49
     * @param string $version Full version (ex : 1.2.3)
50
     * @throws VersionException
51
     */
52
    public function set($version)
53
    {
54
        $parts = explode('.', $version);
55
        if (count($parts) != 3) {
56
            throw new VersionException('Version "' . $version . '" must be like x.y.z.');
57
        }
58
        foreach ($parts as $part) {
59
            if ($part != (string) intval($part)) {
60
                throw new VersionException('Version "' . $version . '" must be like x.y.z, with x, y and z are only numerics.');
61
            }
62
        }
63
        $this->major = $parts[0];
64
        $this->minor = $parts[1];
65
        $this->patch = $parts[2];
66
    }
67
68
    /**
69
     * Get full version (ex : 1.2.3)
70
     *
71
     * @return type
72
     */
73
    public function asString()
74
    {
75
        return $this->getMajor() . '.' . $this->getMinor() . '.' . $this->getPatch();
76
    }
77
78
    /**
79
     * Return major part of the version
80
     *
81
     * @return int
82
     */
83
    public function getMajor()
84
    {
85
        return $this->major;
86
    }
87
88
    /**
89
     * Return minor part of the version
90
     *
91
     * @return string
92
     */
93
    public function getMinor()
94
    {
95
        return $this->minor;
96
    }
97
98
    /**
99
     * Return patch part of the version
100
     *
101
     * @return int
102
     */
103
    public function getPatch()
104
    {
105
        return $this->patch;
106
    }
107
108
    /**
109
     * Define date
110
     *
111
     * @param \DateTime $date
112
     */
113
    public function setDate(\DateTime $date = null)
114
    {
115
        $this->date = $date;
116
    }
117
118
    /**
119
     * Get date
120
     *
121
     * @return \DateTime
122
     */
123
    public function getDate()
124
    {
125
        return $this->date;
126
    }
127
}
128