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\Comment; |
18
|
|
|
use Webuni\Srazy\Model\Event; |
19
|
|
|
use Webuni\Srazy\Model\Poll; |
20
|
|
|
use Webuni\Srazy\Model\Series; |
21
|
|
|
use Webuni\Srazy\Model\Session; |
22
|
|
|
use Webuni\Srazy\Model\User; |
23
|
|
|
|
24
|
|
|
class EventPage extends AbstractPage |
25
|
|
|
{ |
26
|
|
View Code Duplication |
public function getSeries() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$uri = $this->crawler->getUri(); |
29
|
|
|
|
30
|
|
|
$series = $this->client->model(Series::class, $uri); |
31
|
|
|
$series->setUri($uri); |
32
|
|
|
$series->setName(trim($this->crawler->filter('h1.event-title')->text())); |
33
|
|
|
$series->setDescription(trim($this->crawler->filter('.event-header .event-desc-inner')->html())); |
34
|
|
|
|
35
|
|
|
return $series; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getEvent() |
39
|
|
|
{ |
40
|
|
|
$uri = $this->crawler->getUri(); |
41
|
|
|
/* @var $event Event */ |
42
|
|
|
$event = $this->client->model(Event::class, $uri); |
43
|
|
|
|
44
|
|
|
$name = $this->crawler->filter('h2.date-title'); |
45
|
|
|
if (!count($name)) { |
46
|
|
|
$name = $this->crawler->filter('h1.event-title'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$startNode = $this->crawler->filter('time.dtstart'); |
50
|
|
|
$placeNode = $this->crawler->filter('.event-location-name'); |
51
|
|
|
|
52
|
|
|
if (!$startNode->isEmpty()) { |
53
|
|
|
$event->setStart(new \DateTime($startNode->attr('datetime'), new \DateTimeZone('Europe/Prague'))); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!$placeNode->isEmpty()) { |
57
|
|
|
$event->setLocation($placeNode->croppedText()); |
58
|
|
|
$event->setMapUrl($this->crawler->filter('.pull-left h3.event-dateplace a')->link()->getUri()); |
59
|
|
|
$event->setAddress($this->crawler->filter('span[itemprop="address"]')->text()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$event->setName($name->text()); |
63
|
|
|
|
64
|
|
|
$description = ''; |
65
|
|
|
/* @var $node \DOMElement */ |
66
|
|
|
foreach ($this->crawler->filter('#snippet--attending ~ hr')->nextAll() as $node) { |
67
|
|
|
if ('hr' === $node->nodeName) { |
68
|
|
|
break; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$description .= $node->ownerDocument->saveHTML($node); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$event->setDescription($description); |
75
|
|
|
$event->setSessions($this->getSessions($event)); |
76
|
|
|
$event->setPolls($this->getPolls($event)); |
77
|
|
|
$event->setComments($this->getComments($event)); |
78
|
|
|
|
79
|
|
|
return $event; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getSessions(Event $event) |
83
|
|
|
{ |
84
|
|
|
$sessions = new ArrayCollection(); |
85
|
|
|
|
86
|
|
|
$this->crawler->filter('#snippet--program dt')->each(function (Crawler $dt) use ($sessions, $event) { |
87
|
|
|
$session = new Session(); |
88
|
|
|
$session->setEvent($event); |
89
|
|
|
|
90
|
|
|
$time = new \DateTime($dt->text(), new \DateTimeZone('Europe/Prague')); |
91
|
|
|
$date = clone $event->getStart(); |
92
|
|
|
$date->setTime($time->format('H'), $time->format('i'), $time->format('s')); |
93
|
|
|
|
94
|
|
|
$session->setStart($date); |
95
|
|
|
if ($previousSession = $sessions->last()) { |
96
|
|
|
$previousSession->setStop($date); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$sessions->add($session); |
100
|
|
|
}); |
101
|
|
|
|
102
|
|
|
$this->crawler->filter('#snippet--program dd')->each(function (Crawler $dd, $i) use ($sessions) { |
103
|
|
|
$session = $sessions[$i]; |
104
|
|
|
if (preg_match('/program-type-([^ ]+)/', $dd->attr('class'), $matches)) { |
105
|
|
|
$session->setType($matches[1]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$title = $dd->filter('.program-title'); |
109
|
|
|
$speaker = $dd->filter('.program-speaker'); |
110
|
|
|
$description = $dd->filter('.program-desc'); |
111
|
|
|
|
112
|
|
|
if (count($title)) { |
113
|
|
|
$session->setTitle(trim($title->text())); |
114
|
|
|
} |
115
|
|
|
if (count($speaker)) { |
116
|
|
|
$session->setSpeaker(trim($speaker->text())); |
117
|
|
|
} |
118
|
|
|
if (count($description)) { |
119
|
|
|
$session->setDescription(trim($description->text())); |
120
|
|
|
} |
121
|
|
|
}); |
122
|
|
|
|
123
|
|
|
return $sessions; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getPolls($event) |
127
|
|
|
{ |
128
|
|
|
$polls = $this->crawler->filter('.poll')->each(function (Crawler $node) use ($event) { |
129
|
|
|
$poll = new Poll(); |
130
|
|
|
$poll->setEvent($event); |
131
|
|
|
$poll->setName(trim($node->filter('.pollName')->text())); |
132
|
|
|
$data = $node->filter('.pollRow')->each(function ($node) { |
133
|
|
|
return [ |
134
|
|
|
trim($node->filter('.pollRowText')->text()), |
135
|
|
|
trim($node->filter('.progress')->text()), |
136
|
|
|
]; |
137
|
|
|
}); |
138
|
|
|
$poll->setChoices(array_combine(array_column($data, 0), array_column($data, 1))); |
139
|
|
|
|
140
|
|
|
return $poll; |
141
|
|
|
}); |
142
|
|
|
|
143
|
|
|
return new ArrayCollection($polls); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getComments($event) |
147
|
|
|
{ |
148
|
|
|
$comments = $this->crawler->filter('.comments .comment')->each(function (Crawler $node) use ($event) { |
149
|
|
|
$date = trim($node->filter('.comment-date')->text()); |
150
|
|
|
$date = \DateTime::createFromFormat('d. m. Y v H:i', $date, new \DateTimeZone('Europe/Prague')); |
151
|
|
|
|
152
|
|
|
$authorNode = $node->filter('.comment-name'); |
153
|
|
|
$authorUri = $authorNode->link()->getUri(); |
154
|
|
|
|
155
|
|
|
$author = $this->client->model(User::class, $authorUri); |
156
|
|
|
$author->setUri($authorUri); |
157
|
|
|
$author->setName(trim($authorNode->text())); |
158
|
|
|
|
159
|
|
|
$text = ''; |
160
|
|
|
if (preg_match('/<\\/b>:(.+?)<p class="comment-footer/is', $node->filter('.comment-bubble')->html(), $matches)) { |
161
|
|
|
$text = trim($matches[1]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$comment = new Comment(); |
165
|
|
|
$comment->setEvent($event); |
166
|
|
|
$comment->setDate($date); |
|
|
|
|
167
|
|
|
$comment->setAuthor($author); |
168
|
|
|
$comment->setText($text); |
169
|
|
|
|
170
|
|
|
return $comment; |
171
|
|
|
}); |
172
|
|
|
|
173
|
|
|
return new ArrayCollection(array_reverse($comments)); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
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.