Completed
Push — master ( 0dfa5b...38abdd )
by Karl
02:42
created

JobsMulti::getJobsByProvider()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 3
nop 1
crap 2.0185
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 'Dice':
113 2
                    $query->set('text', $keyword);
114 2
                    break;
115 2
                case 'Github':
116 2
                    $query->set('search', $keyword);
117 2
                    break;
118 2
                case 'Govt':
119 2
                    $query->set('query', $keyword);
120 2
                    break;
121 2
                case 'Indeed':
122 2
                    $query->set('q', $keyword);
123 2
                    break;
124 2
                case 'Juju':
125 2
                    $query->set('k', $keyword);
126 2
                    break;
127 2
                case 'Usajobs':
128 2
                    $query->set('Keyword', $keyword);
129 2
                    break;
130 2
                case 'Ziprecruiter':
131 2
                    $query->set('search', $keyword);
132 2
                    break;
133
                default:
134
                    throw new \Exception("Provider {$provider} not found");
135
            }
136 2
        }
137 2
        return $this;
138
    }
139
140
    /**
141
     * Sets a location on the query for each provider.
142
     *
143
     * @param $location
144
     *
145
     * @return $this
146
     */
147 4
    public function setLocation($location)
148
    {
149 4
        if (!$this->isValidLocation($location)) {
150 2
            throw new \OutOfBoundsException("Location parameter must follow the pattern 'City, ST'.");
151
        }
152
153 2
        $locationArr = explode(', ', $location);
154 2
        $city = $locationArr[0];
155 2
        $state = $locationArr[1];
156
157 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...
158
            switch ($provider) {
159 2
                case 'Careerbuilder':
160 2
                    $query->set('Location', $location);
161 2
                    break;
162 2
                case 'Careercast':
163 2
                    $query->set('location', $location);
164 2
                    break;
165 2
                case 'Dice':
166 2
                    $query->set('city', $city);
167 2
                    $query->set('state', $state);
168 2
                    break;
169 2
                case 'Github':
170 2
                    $query->set('location', $location);
171 2
                    break;
172 2
                case 'Govt':
173 2
                    $queryString = $query->get('query').' in '.$location;
174 2
                    $query->set('query', $queryString);
175 2
                    break;
176 2
                case 'Indeed':
177 2
                    $query->set('l', $location);
178 2
                    break;
179 2
                case 'Juju':
180 2
                    $query->set('l', $location);
181 2
                    break;
182 2
                case 'Usajobs':
183 2
                    $query->set('LocationName', $location);
184 2
                    break;
185 2
                case 'Ziprecruiter':
186 2
                    $query->set('location', $location);
187 2
                    break;
188
                default:
189
                    throw new \Exception("Provider {$provider} not found");
190
            }
191 2
        }
192 2
        return $this;
193
    }
194
195
    /**
196
     * Sets a page number and number of results per page for each provider.
197
     *
198
     * @param $page integer
199
     * @param $perPage integer
200
     *
201
     * @return $this
202
     */
203 2
    public function setPage($page = 1, $perPage = 10)
204
    {
205 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...
206
            switch ($provider) {
207 2
                case 'Careerbuilder':
208 2
                    $query->set('PageNumber', $page);
209 2
                    $query->set('PerPage', $perPage);
210 2
                    break;
211 2
                case 'Careercast':
212 2
                    $query->set('page', $page);
213 2
                    $query->set('rows', $perPage);
214 2
                    break;
215 2
                case 'Dice':
216 2
                    $query->set('page', $page);
217 2
                    $query->set('pgcnt', $perPage);
218 2
                    break;
219 2
                case 'Github':
220 2
                    $query->set('page', $page-1);
221
                    // No per_page option
222 2
                    break;
223 2
                case 'Govt':
224 2
                    $query->set('size', $perPage);
225 2
                    $query->set('from', $this->getStartFrom($page, $perPage));
226 2
                    break;
227 2
                case 'Indeed':
228 2
                    $query->set('limit', $perPage);
229 2
                    $query->set('start', $this->getStartFrom($page, $perPage));
230 2
                    break;
231 2
                case 'Juju':
232 2
                    $query->set('page', $page);
233 2
                    $query->set('jpp', $perPage);
234 2
                    break;
235 2
                case 'Usajobs':
236 2
                    $query->set('Page', $page);
237 2
                    $query->set('ResultsPerPage', $perPage);
238 2
                    break;
239 2
                case 'Ziprecruiter':
240 2
                    $query->set('page', $page);
241 2
                    $query->set('jobs_per_page', $perPage);
242 2
                    break;
243
                default:
244
                    throw new \Exception("Provider {$provider} not found");
245
            }
246 2
        }
247 2
        return $this;
248
    }
249
250
    /**
251
     * Instantiates a provider using a query object.
252
     *
253
     * @param null $name
254
     * @param AbstractQuery $query
255
     *
256
     * @return AbstractProvider
257
     */
258 2
    public static function createProvider($name, AbstractQuery $query)
259
    {
260 2
        return new $name($query);
261
    }
262
263
    /**
264
     * Gets a start from count for APIs that use per_page and start_from pattern.
265
     *
266
     * @param int $page
267
     * @param int $perPage
268
     *
269
     * @return int
270
     */
271 2
    private function getStartFrom($page = 1, $perPage = 10)
272
    {
273 2
        return ($page * $perPage) - $perPage;
274
    }
275
276
    /**
277
     * Tests whether location string follows valid convention (City, ST).
278
     *
279
     * @param string $location
280
     *
281
     * @return bool
282
     */
283 4
    private function isValidLocation($location = null)
284
    {
285 4
        preg_match("/([^,]+),\s*(\w{2})/", $location, $matches);
286 4
        return isset($matches[1]) && isset($matches[2]) ? true : false;
287
    }
288
289
    /**
290
     * Tests whether the method is a valid get<Provider>Jobs() method.
291
     *
292
     * @param $method
293
     *
294
     * @return bool
295
     */
296 2
    private function isGetJobsByProviderMethod($method)
297
    {
298 2
        return preg_match('/(get)(.*?)(Jobs)/', $method, $matches) && $matches[2] && isset($this->queries[$matches[2]]);
299
    }
300
301
    /**
302
     * Get the provider name from the method.
303
     *
304
     * @param $method
305
     *
306
     * @return string
307
     */
308
    private function getProviderFromMethod($method)
309
    {
310
        preg_match('/(get)(.*?)(Jobs)/', $method, $matches);
311
        return $matches[2];
312
    }
313
}
314