NpmStats::getStats()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 3
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
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
     * @param string $resource
74
     * @param array $query
75
     *
76
     * @return array
77
     */
78
    private function makeRequest($resource, array $query = [])
79
    {
80
        $packages = $this->client
81
            ->get("{$this->baseUrl}{$resource}", compact('query'))
82
            ->getBody()
83
            ->getContents();
84
85
        return json_decode($packages, true);
86
    }
87
88
    private function replaceSpecialPeriodsIfApplicable(&$period)
89
    {
90
        if ($period === self::TOTAL) {
91
            $currentDate = (new \DateTime)->format('Y-m-d');
92
            $period = "2015-01-01:{$currentDate}";
93
        } elseif ($period === self::LAST_YEAR) {
94
            $currentDate = (new \DateTime)->format('Y-m-d');
95
            $beforeDate = (new \DateTime)->modify('-365 days')->format('Y-m-d');
96
            $period = "{$beforeDate}:{$currentDate}";
97
        }
98
    }
99
}
100