Completed
Branch master (f97dd7)
by Nuno
04:04 queued 01:50
created

Search::getEvents()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 0
1
<?php
2
3
namespace LaravelMeetups\Jobs\Catalog;
4
5
use LaravelMeetups\Contracts\Jobs\Search as Contract;
6
use LaravelMeetups\Http\Client\Catalog\Strategy;
7
use LaravelMeetups\Http\Client\Transporter;
8
use LaravelMeetups\Jobs\AbstractSearch;
9
use LaravelMeetups\Http\Client\Query;
10
11
class Search extends AbstractSearch implements Contract
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function execute()
17
    {
18
        if ($maxRadius = $this->input->getArgument('max_radius')) {
19
            $this->config->setMaxRadius($maxRadius);
20
        }
21
22
        return array_map(function($event) {
23
            $dom = $this->dom->load($event->innerHtml);
24
25
            return new Bag(clone $dom, array_map(function($elem) use ($dom) {
26
                return (new $elem)->find($dom);
27
            }, $this->config->getCatalogProviders()));
28
        }, $this->getRows());
29
    }
30
31
    /**
32
     * Returns the catalog in rows.
33
     *
34
     * @return array
35
     */
36
    private function getRows()
37
    {
38
        $radius = $this->config->getRadiusInterval();
39
        $rows = [];
40
41
        while ($radius < $this->config->getMaxRadius()) {
42
            $this->config->setRadius($radius);
43
            $body = (new Transporter)->execute(new Query(new Strategy($this->config)));
44
            $rows = $this->dom->load($body)->getElementsByClass('event-listing-container-li') ?: [];
45
            $radius += $this->config->getRadiusInterval();
46
        }
47
48
        return is_array($rows) ? $rows : $rows->toArray();
49
    }
50
}