1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This is part of the webuni/srazy-api-client. |
5
|
|
|
* |
6
|
|
|
* (c) Martin Hasoň <[email protected]> |
7
|
|
|
* (c) Webuni s.r.o. <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Webuni\Srazy\Page; |
14
|
|
|
|
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
16
|
|
|
use Webuni\Srazy\Crawler; |
17
|
|
|
use Webuni\Srazy\Model\Event; |
18
|
|
|
use Webuni\Srazy\Model\Series; |
19
|
|
|
|
20
|
|
|
class SeriesPage extends AbstractPage |
21
|
|
|
{ |
22
|
|
|
private static $months = [1 => 'led', 'úno', 'bře', 'dub', 'kvě', 'črv', 'čvc', 'srp', 'zář', 'říj', 'lis', 'pro']; |
23
|
|
|
|
24
|
|
View Code Duplication |
public function getSeries() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$uri = preg_replace('~/terminy/?$~', '', $this->crawler->getUri()); |
27
|
|
|
|
28
|
|
|
$series = $this->client->model(Series::class, $uri); |
29
|
|
|
|
30
|
|
|
$series->setUri($uri); |
31
|
|
|
$series->setName(trim($this->crawler->filter('.event-wrap h1.event-title')->text())); |
32
|
|
|
$series->setDescription(trim($this->crawler->filter('.event-wrap .event-desc .event-desc-inner')->html())); |
33
|
|
|
|
34
|
|
|
$this->getEvents($series); |
35
|
|
|
|
36
|
|
|
return $series; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getEvents(Series $series) |
40
|
|
|
{ |
41
|
|
|
$events = $this->crawler->filter('.event-main .row .row')->each(function (Crawler $row) use ($series) { |
42
|
|
|
$uri = $row->filter('h4 a')->link()->getUri(); |
43
|
|
|
$event = $this->client->model(Event::class, $uri); |
44
|
|
|
$event->setSeries($series); |
45
|
|
|
|
46
|
|
|
$event->setUri($uri); |
47
|
|
|
$event->setName(trim($row->filter('h4 a')->text())); |
48
|
|
|
|
49
|
|
|
$day = trim($row->filter('.datelist-day')->text()); |
50
|
|
|
$month = self::convertMonth(trim($row->filter('.datelist-month')->text())); |
51
|
|
|
$year = trim($row->filter('.datelist-year')->text()); |
52
|
|
|
|
53
|
|
|
$event->setStart(new \DateTime(sprintf('%d-%d-%d', $day, $month, $year), new \DateTimeZone('Europe/Prague'))); |
54
|
|
|
|
55
|
|
|
return $event; |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
$events = new ArrayCollection($events); |
59
|
|
|
$series->setEvents($events); |
60
|
|
|
|
61
|
|
|
return $events; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private static function convertMonth($month) |
65
|
|
|
{ |
66
|
|
|
return array_search($month, self::$months, true); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.