Completed
Push — master ( 156cbf...ec5f3d )
by Frederick
14s
created

DownloadStatistics::getStartDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Vanbrabantf\NpmStatFetcher\Statistics;
4
5
use Cake\Chronos\Chronos;
6
use DateTimeImmutable;
7
use Vanbrabantf\NpmStatFetcher\Package\Package;
8
9
class DownloadStatistics extends Statistics implements StatisticInterface
10
{
11
    /**
12
     * @var int
13
     */
14
    private $downloads;
15
16
    /**
17
     * @var DateTimeImmutable
18
     */
19
    private $startDate;
20
21
    /**
22
     * @var DateTimeImmutable
23
     */
24
    private $endDate;
25
26
    /**
27
     * @param Package $package
28
     * @param int $downloads
29
     * @param DateTimeImmutable $startDate
30
     * @param DateTimeImmutable $endDate
31
     */
32
    public function __construct(
33
        Package $package,
34
        int $downloads,
35
        DateTimeImmutable $startDate,
36
        DateTimeImmutable $endDate
37
    ) {
38
        parent::__construct($package);
39
40
        $this->downloads = $downloads;
41
        $this->startDate = $startDate;
42
        $this->endDate = $endDate;
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function __toString()
49
    {
50
        return (string) $this->downloads;
51
    }
52
53
    /**
54
     * @param Package $package
55
     * @param string $resource
56
     *
57
     * @return DownloadStatistics
58
     */
59
    public static function fromJson(Package $package, string $resource): DownloadStatistics
60
    {
61
        $resourceArray = json_decode($resource);
62
63
        return new self(
64
            $package,
65
            $resourceArray->downloads,
66
            new Chronos($resourceArray->start),
67
            new Chronos($resourceArray->end)
68
        );
69
    }
70
71
    /**
72
     * @return int
73
     */
74
    public function getDownloads(): int
75
    {
76
        return $this->downloads;
77
    }
78
79
    /**
80
     * @return DateTimeImmutable
81
     */
82
    public function getStartDate()
83
    {
84
        return $this->startDate;
85
    }
86
87
    /**
88
     * @return DateTimeImmutable
89
     */
90
    public function getEndDate()
91
    {
92
        return $this->endDate;
93
    }
94
}
95