Completed
Push — master ( 38abdd...d4c9bb )
by Karl
03:15
created

JobsMulti::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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