Completed
Push — master ( 98fc75...1c7a02 )
by Rémy
03:43
created

Booking::findAll()   C

Complexity

Conditions 8
Paths 20

Size

Total Lines 38
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 19
nc 20
nop 4
1
<?php
2
3
namespace XoteliaClient\Endpoint;
4
5
use Doctrine\Common\Annotations\AnnotationRegistry;
6
use JMS\Serializer\Serializer;
7
use JMS\Serializer\SerializerBuilder;
8
use XoteliaClient\Client;
9
use XoteliaClient\Model\Booking as BookingModel;
10
11
class Booking extends AbstractEndpoint
12
{
13
    const ENDPOINT = 'bookings';
14
    const ERROR_NO_BOOKINGS_IN_DATABASE = 12;
15
16
    /**
17
     * @var Serializer
18
     */
19
    public $serializer;
20
21
    /**
22
     * @param Client $client
23
     */
24
    public function __construct(Client $client)
25
    {
26
        parent::__construct($client);
27
28
        AnnotationRegistry::registerLoader('class_exists');
29
        $this->serializer = SerializerBuilder::create()->build();
30
    }
31
32
    /**
33
     * Find created, updated or cancelled bookings between $startDate and $endDate
34
     * for hotelId (optional) and roomId (optional)
35
     *
36
     * @param \DateTime $startDate
37
     * @param \DateTime $endDate
38
     * @param int|null $hotelId
39
     * @param int|null $roomId
40
     *
41
     * @return array|null
42
     * @throws \Exception
43
     */
44
    public function findAll(\DateTime $startDate, \DateTime $endDate, $hotelId = null, $roomId = null)
45
    {
46
        $query = [
47
            'start_date' => $startDate->format('Y-m-d'),
48
            'end_date'   => $endDate->format('Y-m-d'),
49
        ];
50
51
        if (null !== $hotelId) {
52
            $query['hotelId'] = $hotelId;
53
        }
54
55
        if (null !== $roomId) {
56
            $query['roomId'] = $roomId;
57
        }
58
59
        $result = $this->client->get(self::ENDPOINT, [
60
            'query' => $query
61
        ]);
62
63
        if (isset($result['error']) && 403 === $result['error']['code']) {
64
            throw new \Exception($result['error']['message'], $result['error']['code']);
65
        }
66
67
        if (false === $result['success']) {
68
69
            if (self::ERROR_NO_BOOKINGS_IN_DATABASE === $result['error']['code']) {
70
                return [];
71
            }
72
73
            throw new \Exception($result['error']['message'], $result['error']['code']);
74
        }
75
76
        if (!isset($result['bookings'])) {
77
            return [];
78
        }
79
80
        return $this->serializer->deserialize(json_encode($result['bookings']), 'array<XoteliaClient\Model\Booking>', 'json');
81
    }
82
83
    /**
84
     * @param BookingModel $booking
85
     *
86
     * @return int
87
     * @throws \Exception
88
     */
89
    public function set(BookingModel $booking)
90
    {
91
        $options = [
92
            'body' => $this->serializer->serialize($booking, 'json')
93
        ];
94
95
        switch ($booking->getStatus())
96
        {
97
            case BookingModel::STATUS_NEW:
98
                $result = $this->client->post(self::ENDPOINT, $options);
99
                break;
100
            case BookingModel::STATUS_MODIFIED:
101
                $result = $this->client->put(self::ENDPOINT, $options);
102
                break;
103
            case BookingModel::STATUS_CANCELLED:
104
                $result = $this->client->delete(self::ENDPOINT, $options);
105
                break;
106
            default:
107
                throw new \Exception('You need to provide a status to your booking');
108
        };
109
110
        if (false === $result['success']) {
111
            throw new \Exception($result['error']['message'], $result['error']['code']);
112
        }
113
114
        return $result['booking_id'];
115
    }
116
117
    /**
118
     * @param int $internalId
119
     *
120
     * @return bool
121
     * @throws \Exception
122
     */
123
    public function confirm($internalId)
124
    {
125
        $result = $this->client->get(sprintf('%s/confirmation/%d', self::ENDPOINT, $internalId));
126
127
        if (false === $result['success']) {
128
            throw new \Exception($result['message']);
129
        }
130
131
        return true;
132
    }
133
}
134
135