ProjectDataProvider   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
c 0
b 0
f 0
lcom 1
cbo 9
dl 0
loc 156
ccs 0
cts 92
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 9 2
F prepareModels() 0 72 14
A prepareKeys() 0 13 2
A prepareTotalCount() 0 5 1
A setSort() 0 20 2
1
<?php
2
/**
3
 * Asset Packagist.
4
 *
5
 * @link      https://github.com/hiqdev/asset-packagist
6
 * @package   asset-packagist
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\assetpackagist\librariesio;
12
13
use GuzzleHttp\Psr7\Response;
14
use Yii;
15
use yii\caching\Cache;
16
use yii\data\BaseDataProvider;
17
use yii\di\Instance;
18
use yii\helpers\Json;
19
20
class ProjectDataProvider extends BaseDataProvider
21
{
22
    public $query;
23
    public $platform;
24
25
    /**
26
     * Enable cache to search results, false to disable
27
     * Cache is used to prevent rate limit.
28
     * @see https://libraries.io/api/#rate-limit
29
     * @var string|Cache|false
30
     */
31
    public $cache = 'cache';
32
33
    /**
34
     * @var int number of seconds that search result can remain valid in cache
35
     */
36
    public $cacheDuration = 600;
37
38
    /**
39
     * @var string|LibrariesioRepository
40
     */
41
    public $repository = 'librariesio';
42
43
    /**
44
     * @var int Total count from last request
45
     */
46
    protected $lastTotalCount = 0;
47
48
    public function init()
49
    {
50
        parent::init();
51
52
        if ($this->cache) {
53
            $this->cache = Instance::ensure($this->cache, Cache::className());
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
54
        }
55
        $this->repository = Instance::ensure($this->repository, LibrariesioRepository::className());
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
56
    }
57
58
    protected function prepareModels()
59
    {
60
        if (!$this->platform) {
61
            $this->platform = 'bower,npm';
62
        }
63
        if (is_array($this->platform)) {
64
            $this->platform = implode(',', $this->platform);
65
        }
66
67
        $query = [
68
            'q'         => $this->query,
69
            'platforms' => $this->platform,
70
        ];
71
72
        if (($sort = $this->getSort()) !== false) {
73
            foreach ($sort->getOrders() as $sort => $order) {
74
                $query['sort'] = $sort;
75
                $query['order'] = ($order === SORT_ASC) ? 'asc' : 'desc';
76
                break;
77
            }
78
        }
79
80
        if (($pagination = $this->getPagination()) !== false) {
81
            /**
82
             * Not use '$pagination->getPage()' because need define 'totalCount' first
83
             * totalCount is defined after request.
84
             */
85
            $page = (int) Yii::$app->getRequest()->getQueryParam($pagination->pageParam, 1);
86
            if ($page > 1) {
87
                $query['page'] = $page;
88
            }
89
90
            $query['per_page'] = $pagination->getPageSize();
91
        }
92
93
        $cacheKey = [static::className(), 'query' => $query];
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
94
95
        $items = [];
96
        $totalCount = 0;
97
98
        if ($this->cache && $this->cache->exists($cacheKey)) {
99
            list($totalCount, $items) = $this->cache->get($cacheKey);
100
        } else {
101
            /* @var $response Response */
102
            $response = $this->repository->search($query);
103
104
            if ($response->getStatusCode() !== 200) {
105
                return [];
106
            }
107
108
            $totalCount = (int) $response->getHeaderLine('total');
109
            $items = Json::decode($response->getBody());
110
111
            if ($this->cache) {
112
                $this->cache->set($cacheKey, [$totalCount, $items], $this->cacheDuration);
113
            }
114
        }
115
116
        $this->lastTotalCount = $totalCount;
117
118
        if (($pagination = $this->getPagination()) !== false) {
119
            $pagination->totalCount = $this->lastTotalCount;
120
        }
121
122
        $models = [];
123
124
        foreach ($items as $item) {
125
            $models[] = new Project(['attributes' => $item]);
126
        }
127
128
        return $models;
129
    }
130
131
    /**
132
     * @param Project[] $models
133
     * @return array
134
     */
135
    protected function prepareKeys($models)
136
    {
137
        $keys = [];
138
139
        foreach ($models as $model) {
140
            $keys[] = [
141
                'name'     => $model->name,
142
                'platform' => $model->name,
143
            ];
144
        }
145
146
        return $keys;
147
    }
148
149
    protected function prepareTotalCount()
150
    {
151
        $this->prepare();
152
        return $this->lastTotalCount;
153
    }
154
155
    public function setSort($value)
156
    {
157
        /**
158
         * Override the default attributes to sort.
159
         */
160
        if (is_array($value)) {
161
            $value = array_merge([
162
                'attributes' => [
163
                    'Rank'                        => 'rank',
164
                    'Stars'                       => 'stars',
165
                    'Dependents Count'            => 'dependents_count',
166
                    'Dependent Repos Count'       => 'dependent_repos_count',
167
                    'Latest Release Published At' => 'latest_release_published_at',
168
                    'Created At'                  => 'created_at',
169
                    'Contributions Count'         => 'contributions_count',
170
                ], ], $value);
171
        }
172
173
        parent::setSort($value);
174
    }
175
}
176