Completed
Push — master ( 0b96ba...cc60ec )
by Freek
11s
created

Packagist::getPackagesByType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Spatie\Packagist;
4
5
use Exception;
6
use GuzzleHttp\Client;
7
8
class Packagist
9
{
10
    /** @var \GuzzleHttp\Client */
11
    protected $client;
12
13
    /** @var string */
14
    protected $baseUrl;
15
16
    /**
17
     * @param \GuzzleHttp\Client $client
18
     * @param string             $baseUrl
19
     */
20
    public function __construct(Client $client, $baseUrl = 'https://packagist.org')
21
    {
22
        $this->client = $client;
23
24
        $this->baseUrl = $baseUrl;
25
    }
26
27
    /**
28
     * @param string $type
29
     *
30
     * @return array
31
     */
32
    public function getPackagesByType($type)
33
    {
34
        if (empty($type)) {
35
            throw new Exception('You must pass a non-empty value');
36
        }
37
38
        return $this->makeRequest('/packages/list.json', compact('type'));
39
    }
40
41
    /**
42
     * @param string $vendor
43
     *
44
     * @return array
45
     */
46
    public function getPackagesByVendor($vendor)
47
    {
48
        if (empty($vendor)) {
49
            throw new Exception('You must pass a non empty value');
50
        }
51
52
        return $this->makeRequest('/packages/list.json', compact('vendor'));
53
    }
54
55
    /**
56
     * @param string $name
57
     *
58
     * @return array
59
     */
60
    public function getPackagesByName($name)
61
    {
62
        return $this->makeRequest('/search.json', ['q' => $name]);
63
    }
64
65
    /**
66
     * @param $vendor
67
     * @param string $packageName
68
     *
69
     * @return array
70
     */
71
    public function findPackageByName($vendor, $packageName = '')
72
    {
73
        if ($packageName === '') {
74
            list($vendor, $packageName) = explode('/', $vendor);
75
        }
76
77
        return $this->makeRequest("/packages/{$vendor}/{$packageName}.json");
78
    }
79
80
    /**
81
     * @param string $resource
82
     * @param array  $query
83
     *
84
     * @return array
85
     */
86
    public function makeRequest($resource, array $query = [])
87
    {
88
        $packages = $this->client
89
            ->get("{$this->baseUrl}{$resource}", compact('query'))
90
            ->getBody()
91
            ->getContents();
92
93
        return json_decode($packages, true);
94
    }
95
96
    /**
97
     * @param string $name
98
     *
99
     * @return array
100
     */
101
    public function getPackageMetadata($name)
102
    {
103
        if ($name === '') {
104
            throw new Exception('You must pass a non empty value');
105
        }
106
107
        $package = explode('/', $name);
108
109
        return $this->makeRequest("/packages/{$package[0]}/{$package[1]}.json");
110
    }
111
}
112