Completed
Pull Request — master (#8)
by Frederick
07:52
created

DownloadStatistics::fromJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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