Completed
Push — master ( 59521c...2d99b9 )
by Karl
02:27
created

JobsMulti::createProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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