Completed
Push — master ( 04809c...7fd40a )
by Karl
03:23
created

JobsMulti::setKeyword()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 43
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 13.33

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 35
cts 40
cp 0.875
rs 5.1234
c 0
b 0
f 0
cc 13
eloc 39
nc 13
nop 1
crap 13.33

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace JobApis\Jobs\Client;
2
3
use JobApis\Jobs\Client\Providers\AbstractProvider;
4
use JobApis\Jobs\Client\Queries\AbstractQuery;
5
6
class JobsMulti
7
{
8
    /**
9
     * Job board API query objects
10
     *
11
     * @var AbstractQuery
12
     */
13
    protected $queries = [];
14
15
    /**
16
     * Creates query objects for each provider and creates this unified client.
17
     *
18
     * @param array $providers
19
     */
20 18
    public function __construct($providers = [])
21
    {
22 18
        foreach ($providers as $provider => $options) {
23 18
            $query = $provider.'Query';
24 18
            $className = 'JobApis\\Jobs\\Client\\Queries\\'.$query;
25 18
            $this->addQuery($provider, $className, $options);
26 18
        }
27 18
    }
28
29
    /**
30
     * Overrides get<Provider>Jobs() methods
31
     *
32
     * @param $method
33
     * @param $args
34
     *
35
     * @return mixed
36
     */
37 2
    public function __call($method, $args)
38
    {
39 2
        if ($this->isGetJobsByProviderMethod($method)) {
40
            return $this->getJobsByProvider($this->getProviderFromMethod($method));
41
        }
42
43 2
        throw new \BadMethodCallException(sprintf(
44 2
            '%s does not contain a method by the name of "%s"',
45 2
            __CLASS__,
46
            $method
47 2
        ));
48
    }
49
50
    /**
51
     * Instantiates a Query object and adds it to the queries array.
52
     *
53
     * @param $key string Query name
54
     * @param $className string Query class name
55
     * @param $options array Parameters to add to constructor
56
     *
57
     * @return $this
58
     */
59 18
    public function addQuery($key, $className, $options = [])
60
    {
61 18
        $this->queries[$key] = new $className($options);
62 18
        return $this;
63
    }
64
65
    /**
66
     * Gets jobs from all providers in a single go and returns an array of Collection objects.
67
     *
68
     * @return array
69
     */
70
    public function getAllJobs()
71
    {
72
        $jobs = [];
73
        foreach ($this->queries as $key => $query) {
0 ignored issues
show
Bug introduced by
The expression $this->queries of type object<JobApis\Jobs\Client\Queries\AbstractQuery> is not traversable.
Loading history...
74
            $jobs[$key] = $this->getJobsByProvider($key);
75
        }
76
        return $jobs;
77
    }
78
79
    /**
80
     * Gets jobs from a single provider and hydrates a new jobs collection.
81
     *
82
     * @return \JobApis\Jobs\Client\Collection
83
     */
84 2
    public function getJobsByProvider($provider)
85
    {
86
        try {
87 2
            $providerName = 'JobApis\\Jobs\\Client\\Providers\\' . $provider . 'Provider';
88 2
            $client = self::createProvider($providerName, $this->queries[$provider]);
89
            return $client->getJobs();
90 2
        } catch (\Exception $e) {
91 2
            return (new Collection())->addError($e->getMessage());
92
        }
93
    }
94
95
    /**
96
     * Sets a keyword on the query for each provider.
97
     *
98
     * @param $keyword string
99
     *
100
     * @return $this
101
     */
102 2
    public function setKeyword($keyword)
103
    {
104 2
        foreach ($this->queries as $provider => $query) {
0 ignored issues
show
Bug introduced by
The expression $this->queries of type object<JobApis\Jobs\Client\Queries\AbstractQuery> is not traversable.
Loading history...
105
            switch ($provider) {
106 2
                case 'Careerbuilder':
107 2
                    $query->set('Keywords', $keyword);
108 2
                    break;
109 2
                case 'Careercast':
110 2
                    $query->set('keyword', $keyword);
111 2
                    break;
112 2
                case 'Careerjet':
113
                    $query->set('keywords', $keyword);
114
                    break;
115 2
                case 'Dice':
116 2
                    $query->set('text', $keyword);
117 2
                    break;
118 2
                case 'Github':
119 2
                    $query->set('search', $keyword);
120 2
                    break;
121 2
                case 'Govt':
122 2
                    $query->set('query', $keyword);
123 2
                    break;
124 2
                case 'Indeed':
125 2
                    $query->set('q', $keyword);
126 2
                    break;
127 2
                case 'Jobinventory':
128 2
                    $query->set('q', $keyword);
129 2
                    break;
130 2
                case 'Juju':
131 2
                    $query->set('k', $keyword);
132 2
                    break;
133 2
                case 'Usajobs':
134 2
                    $query->set('Keyword', $keyword);
135 2
                    break;
136 2
                case 'Ziprecruiter':
137 2
                    $query->set('search', $keyword);
138 2
                    break;
139
                default:
140
                    throw new \Exception("Provider {$provider} not found");
141
            }
142 2
        }
143 2
        return $this;
144
    }
145
146
    /**
147
     * Sets a location on the query for each provider.
148
     *
149
     * @param $location
150
     *
151
     * @return $this
152
     */
153 4
    public function setLocation($location)
154
    {
155 4
        if (!$this->isValidLocation($location)) {
156 2
            throw new \OutOfBoundsException("Location parameter must follow the pattern 'City, ST'.");
157
        }
158
159 2
        $locationArr = explode(', ', $location);
160 2
        $city = $locationArr[0];
161 2
        $state = $locationArr[1];
162
163 2
        foreach ($this->queries as $provider => $query) {
0 ignored issues
show
Bug introduced by
The expression $this->queries of type object<JobApis\Jobs\Client\Queries\AbstractQuery> is not traversable.
Loading history...
164
            switch ($provider) {
165 2
                case 'Careerbuilder':
166 2
                    $query->set('Location', $location);
167 2
                    break;
168 2
                case 'Careercast':
169 2
                    $query->set('location', $location);
170 2
                    break;
171 2
                case 'Careerjet':
172
                    $query->set('location', $location);
173
                    break;
174 2
                case 'Dice':
175 2
                    $query->set('city', $city);
176 2
                    $query->set('state', $state);
177 2
                    break;
178 2
                case 'Github':
179 2
                    $query->set('location', $location);
180 2
                    break;
181 2
                case 'Govt':
182 2
                    $queryString = $query->get('query').' in '.$location;
183 2
                    $query->set('query', $queryString);
184 2
                    break;
185 2
                case 'Indeed':
186 2
                    $query->set('l', $location);
187 2
                    break;
188 2
                case 'Jobinventory':
189 2
                    $query->set('l', $location);
190 2
                    break;
191 2
                case 'Juju':
192 2
                    $query->set('l', $location);
193 2
                    break;
194 2
                case 'Usajobs':
195 2
                    $query->set('LocationName', $location);
196 2
                    break;
197 2
                case 'Ziprecruiter':
198 2
                    $query->set('location', $location);
199 2
                    break;
200
                default:
201
                    throw new \Exception("Provider {$provider} not found");
202
            }
203 2
        }
204 2
        return $this;
205
    }
206
207
    /**
208
     * Sets a page number and number of results per page for each provider.
209
     *
210
     * @param $page integer
211
     * @param $perPage integer
212
     *
213
     * @return $this
214
     */
215 2
    public function setPage($page = 1, $perPage = 10)
216
    {
217 2
        foreach ($this->queries as $provider => $query) {
0 ignored issues
show
Bug introduced by
The expression $this->queries of type object<JobApis\Jobs\Client\Queries\AbstractQuery> is not traversable.
Loading history...
218
            switch ($provider) {
219 2
                case 'Careerbuilder':
220 2
                    $query->set('PageNumber', $page);
221 2
                    $query->set('PerPage', $perPage);
222 2
                    break;
223 2
                case 'Careercast':
224 2
                    $query->set('page', $page);
225 2
                    $query->set('rows', $perPage);
226 2
                    break;
227 2
                case 'Careerjet':
228
                    $query->set('page', $page);
229
                    $query->set('pagesize', $perPage);
230
                    break;
231 2
                case 'Dice':
232 2
                    $query->set('page', $page);
233 2
                    $query->set('pgcnt', $perPage);
234 2
                    break;
235 2
                case 'Github':
236 2
                    $query->set('page', $page-1);
237
                    // No per_page option
238 2
                    break;
239 2
                case 'Govt':
240 2
                    $query->set('size', $perPage);
241 2
                    $query->set('from', $this->getStartFrom($page, $perPage));
242 2
                    break;
243 2
                case 'Indeed':
244 2
                    $query->set('limit', $perPage);
245 2
                    $query->set('start', $this->getStartFrom($page, $perPage));
246 2
                    break;
247 2
                case 'Jobinventory':
248 2
                    $query->set('p', $page);
249 2
                    $query->set('limit', $perPage);
250 2
                    break;
251 2
                case 'Juju':
252 2
                    $query->set('page', $page);
253 2
                    $query->set('jpp', $perPage);
254 2
                    break;
255 2
                case 'Usajobs':
256 2
                    $query->set('Page', $page);
257 2
                    $query->set('ResultsPerPage', $perPage);
258 2
                    break;
259 2
                case 'Ziprecruiter':
260 2
                    $query->set('page', $page);
261 2
                    $query->set('jobs_per_page', $perPage);
262 2
                    break;
263
                default:
264
                    throw new \Exception("Provider {$provider} not found");
265
            }
266 2
        }
267 2
        return $this;
268
    }
269
270
    /**
271
     * Instantiates a provider using a query object.
272
     *
273
     * @param null $name
274
     * @param AbstractQuery $query
275
     *
276
     * @return AbstractProvider
277
     */
278 2
    public static function createProvider($name, AbstractQuery $query)
279
    {
280 2
        return new $name($query);
281
    }
282
283
    /**
284
     * Gets a start from count for APIs that use per_page and start_from pattern.
285
     *
286
     * @param int $page
287
     * @param int $perPage
288
     *
289
     * @return int
290
     */
291 2
    private function getStartFrom($page = 1, $perPage = 10)
292
    {
293 2
        return ($page * $perPage) - $perPage;
294
    }
295
296
    /**
297
     * Tests whether location string follows valid convention (City, ST).
298
     *
299
     * @param string $location
300
     *
301
     * @return bool
302
     */
303 4
    private function isValidLocation($location = null)
304
    {
305 4
        preg_match("/([^,]+),\s*(\w{2})/", $location, $matches);
306 4
        return isset($matches[1]) && isset($matches[2]) ? true : false;
307
    }
308
309
    /**
310
     * Tests whether the method is a valid get<Provider>Jobs() method.
311
     *
312
     * @param $method
313
     *
314
     * @return bool
315
     */
316 2
    private function isGetJobsByProviderMethod($method)
317
    {
318 2
        return preg_match('/(get)(.*?)(Jobs)/', $method, $matches) && $matches[2] && isset($this->queries[$matches[2]]);
319
    }
320
321
    /**
322
     * Get the provider name from the method.
323
     *
324
     * @param $method
325
     *
326
     * @return string
327
     */
328
    private function getProviderFromMethod($method)
329
    {
330
        preg_match('/(get)(.*?)(Jobs)/', $method, $matches);
331
        return $matches[2];
332
    }
333
}
334