EventApi::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
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\Api;
14
15
use GuzzleHttp\Psr7\Response;
16
use Psr\Http\Message\ResponseInterface;
17
use Webuni\Srazy\Model\Event;
18
use Webuni\Srazy\Page\AttendeesPage;
19
use Webuni\Srazy\Page\EventPage;
20
21
class EventApi extends AbstractApi
22
{
23
    public function get($event)
24
    {
25
        $url = $event;
26
        if ($event instanceof Event) {
27
            $url = $event->getUri();
28
        }
29
30
        $crawler = $this->client->get($url);
31
        $page = new EventPage($crawler, $this->client);
32
33
        return $page->getEvent();
34
    }
35
36 View Code Duplication
    public function getConfirmedAttendees(Event $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
37
    {
38
        $url = $event->getUri().'?what=yes&do=showAll';
39
40
        $crawler = $this->client->ajax($url, [], function ($response) {
41
            return $this->getAttendeesSnippet($response, 'snippet--visitorsallyes');
42
        });
43
        $page = new AttendeesPage($crawler, $this->client);
44
45
        $attendees = $page->getAttendees();
46
        $event->setConfirmedAttendees($attendees);
47
48
        return $attendees;
49
    }
50
51 View Code Duplication
    public function getUnconfirmedAttendees(Event $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
52
    {
53
        $url = $event->getUri().'?what=maybe&do=showAll';
54
55
        $crawler = $this->client->ajax($url, [], function ($response) {
56
            return $this->getAttendeesSnippet($response, 'snippet--visitorsallmaybe');
57
        });
58
        $page = new AttendeesPage($crawler, $this->client);
59
60
        $attendees = $page->getAttendees();
61
        $event->setUnconfirmedAttendees($attendees);
62
63
        return $attendees;
64
    }
65
66
    private function getAttendeesSnippet(ResponseInterface $response, $key)
67
    {
68
        $json = json_decode((string) $response->getBody(), true);
69
        $html = '<html><head><meta charset="UTF-8"></head><body>'.$json['snippets'][$key].'</body></html>';
70
71
        return new Response($response->getStatusCode(), $response->getHeaders(), $html, $response->getProtocolVersion(), $response->getReasonPhrase());
72
    }
73
}
74