Completed
Push — master ( 4c70b7...633079 )
by Frederick
13s
created

StatFetcher::getDownloadsLastWeek()   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 GuzzleHttp\Client;
6
use Vanbrabantf\NpmStatFetcher\Repositories\NpmRegistryRepository;
7
use Vanbrabantf\NpmStatFetcher\ValueObjects\DownloadStatistics;
8
use Vanbrabantf\NpmStatFetcher\ValueObjects\Package;
9
10
class StatFetcher
11
{
12
    /**
13
     * @var Package
14
     */
15
    private $package;
16
17
    /**
18
     * @var NpmRegistryRepository
19
     */
20
    private $repository;
21
22
    /**
23
     * @param Package $package
24
     * @param NpmRegistryRepository $repository
25
     */
26
    public function __construct(
27
        Package $package,
28
        $repository = null
29
    )
30
    {
31
        $this->package = $package;
32
33
        if (is_null($repository)) {
34
            $this->repository = new NpmRegistryRepository(new Client());
35
        } else {
36
            $this->repository = $repository;
37
        }
38
    }
39
40
    /**
41
     * @return DownloadStatistics
42
     */
43
    public function getDownloadsLastDay()
44
    {
45
        $resource = $this->repository->getResourceByPath(
46
            '/downloads/point/last-day/' . $this->package
47
        );
48
49
        return DownloadStatistics::fromJson($this->package, $resource);
50
    }
51
52
    /**
53
     * @return DownloadStatistics
54
     */
55
    public function getDownloadsLastWeek()
56
    {
57
        $resource = $this->repository->getResourceByPath(
58
            '/downloads/point/last-week/' . $this->package
59
        );
60
61
        return DownloadStatistics::fromJson($this->package, $resource);
62
    }
63
64
    /**
65
     * @return DownloadStatistics
66
     */
67
    public function getDownloadsLastMonth()
68
    {
69
        $resource = $this->repository->getResourceByPath(
70
            '/downloads/point/last-month/' . $this->package
71
        );
72
73
        return DownloadStatistics::fromJson($this->package, $resource);
74
    }
75
76
    /**
77
     * @return DownloadStatistics
78
     */
79
    public function getDownloadsLastYear()
80
    {
81
        $resource = $this->repository->getResourceByPath(
82
            '/downloads/point/last-year/' . $this->package
83
        );
84
85
        return DownloadStatistics::fromJson($this->package, $resource);
86
    }
87
}
88