Completed
Push — master ( 8dede8...df9127 )
by Alexander
01:03
created

NpmStats::replaceSpecialPeriodsIfApplicable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
namespace Developmint\NpmStats;
4
5
use GuzzleHttp\Client;
6
7
class NpmStats
8
{
9
    /** @var \GuzzleHttp\Client */
10
    protected $client;
11
    /** @var string */
12
    protected $baseUrl;
13
14
    const LAST_DAY = "last-day";
15
    const LAST_WEEK = "last-week";
16
    const LAST_MONTH = "last-month";
17
    const LAST_YEAR = "last-year";
18
    const TOTAL = "total";
19
20
    /**
21
     * @param \GuzzleHttp\Client $client
22
     * @param string $baseUrl
23
     */
24
    public function __construct(Client $client, $baseUrl = 'https://api.npmjs.org/downloads')
25
    {
26
        $this->client = $client;
27
        $this->baseUrl = $baseUrl;
28
    }
29
30
    /**
31
     * @param $packageName
32
     * @param string $pointValue
33
     * @return array
34
     */
35
    private function getStatsByPoint($packageName, $pointValue = "last-day")
36
    {
37
        return $this->makeRequest("/point/{$pointValue}/{$packageName}");
38
    }
39
40
    /**
41
     * @param $packageName
42
     * @param string $rangeValue
43
     * @return array
44
     */
45
    private function getStatsByRange($packageName, $rangeValue = "last-day")
46
    {
47
        return $this->makeRequest("/range/{$rangeValue}/{$packageName}");
48
    }
49
50
    /**
51
     * @param $packageName
52
     * @param string $period
53
     * @param bool $asRange
54
     * @return array
55
     * @throws \Exception
56
     */
57
    public function getStats($packageName, $period = "last-day", $asRange = false)
58
    {
59
        if (empty($packageName)) {
60
            throw new \Exception("Package name can't be empty");
61
        }
62
63
        $this->replaceSpecialPeriodsIfApplicable($period);
64
65
        if ($asRange) {
66
            return $this->getStatsByRange($packageName, $period);
67
        }
68
69
        return $this->getStatsByPoint($packageName, $period);
70
    }
71
72
73
    /**
74
     * @param string $resource
75
     * @param array $query
76
     *
77
     * @return array
78
     */
79
    private function makeRequest($resource, array $query = [])
80
    {
81
        $packages = $this->client
82
            ->get("{$this->baseUrl}{$resource}", compact('query'))
83
            ->getBody()
84
            ->getContents();
85
86
        return json_decode($packages, true);
87
    }
88
89
    private function replaceSpecialPeriodsIfApplicable(&$period)
90
    {
91
        if ($period === self::TOTAL) {
92
            $currentDate = (new \DateTime)->format("Y-m-d");
93
            $period = "2015-01-01:{$currentDate}";
94
        } else if ($period === self::LAST_YEAR) {
95
            $currentDate = (new \DateTime)->format("Y-m-d");
96
            $beforeDate = (new \DateTime)->modify("-1 year")->format("Y-m-d");
97
            $period = "{$beforeDate}:{$currentDate}";
98
        }
99
100
        return;
101
    }
102
}