Completed
Push — master ( 633079...840c2c )
by Frederick
11s
created

StatFetcher::getDownloadsLastMonth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Vanbrabantf\NpmStatFetcher;
4
5
use Cake\Chronos\Chronos;
6
use DateTimeInterface;
7
use GuzzleHttp\Client;
8
use Vanbrabantf\NpmStatFetcher\Helpers\DateChecker;
9
use Vanbrabantf\NpmStatFetcher\Repositories\NpmRegistryRepository;
10
use Vanbrabantf\NpmStatFetcher\ValueObjects\DownloadStatistics;
11
use Vanbrabantf\NpmStatFetcher\ValueObjects\Package;
12
13
class StatFetcher
14
{
15
    /**
16
     * @var Package
17
     */
18
    private $package;
19
20
    /**
21
     * @var NpmRegistryRepository
22
     */
23
    private $repository;
24
25
    /**
26
     * @param Package $package
27
     * @param NpmRegistryRepository $repository
28
     */
29
    public function __construct(
30
        Package $package,
31
        $repository = null
32
    )
33
    {
34
        $this->package = $package;
35
36
        if (is_null($repository)) {
37
            $this->repository = new NpmRegistryRepository(new Client());
38
        } else {
39
            $this->repository = $repository;
40
        }
41
    }
42
43
    /**
44
     * @return DownloadStatistics
45
     */
46
    public function getDownloadsLastDay()
47
    {
48
        $resource = $this->repository->getResourceByPath(
49
            '/downloads/point/last-day/' . $this->package
50
        );
51
52
        return DownloadStatistics::fromJson($this->package, $resource);
53
    }
54
55
    /**
56
     * @return DownloadStatistics
57
     */
58
    public function getDownloadsLastWeek()
59
    {
60
        $resource = $this->repository->getResourceByPath(
61
            '/downloads/point/last-week/' . $this->package
62
        );
63
64
        return DownloadStatistics::fromJson($this->package, $resource);
65
    }
66
67
    /**
68
     * @return DownloadStatistics
69
     */
70
    public function getDownloadsLastMonth()
71
    {
72
        $resource = $this->repository->getResourceByPath(
73
            '/downloads/point/last-month/' . $this->package
74
        );
75
76
        return DownloadStatistics::fromJson($this->package, $resource);
77
    }
78
79
    /**
80
     * @return DownloadStatistics
81
     */
82
    public function getDownloadsLastYear()
83
    {
84
        $resource = $this->repository->getResourceByPath(
85
            '/downloads/point/last-year/' . $this->package
86
        );
87
88
        return DownloadStatistics::fromJson($this->package, $resource);
89
    }
90
91
    /**
92
     * @return DownloadStatistics
93
     */
94
    public function getDownloads()
95
    {
96
        $start = new Chronos('1999-01-01');
97
        $now = new Chronos();
98
99
        DateChecker::validateDateRange($start, $now);
100
101
        $resource = $this->repository->getResourceByPath(
102
            '/downloads/point/' . $start->format('Y-m-d') . ':' . $now->format('Y-m-d') . '/' . $this->package
103
        );
104
105
        return DownloadStatistics::fromJson($this->package, $resource);
106
    }
107
108
    /**
109
     * @param DateTimeInterface $start
110
     * @param DateTimeInterface $end
111
     *
112
     * @return DownloadStatistics
113
     */
114
    public function getDownloadsBetweenDates(DateTimeInterface $start, DateTimeInterface $end)
115
    {
116
        DateChecker::validateDateRange($start, $end);
117
118
        $resource = $this->repository->getResourceByPath(
119
            '/downloads/point/' . $start->format('Y-m-d') . ':' . $end->format('Y-m-d') . '/' . $this->package
120
        );
121
122
        return DownloadStatistics::fromJson($this->package, $resource);
123
    }
124
}
125