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

Search   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 0
loc 40
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 14 2
A getRows() 0 14 4
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
}