BookingRepository   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 256
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 256
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findAllByUser() 0 9 1
A findByUserAndMonth() 0 21 1
B isUsersFirstBookingForMonth() 0 24 1
B getMonthBookedToDateTotalByUser() 0 27 2
B getMonthBookedTotalByUser() 0 27 2
B getTotalBookedBetweenByUser() 0 24 2
A find() 0 4 1
A findAll() 0 4 1
A findBy() 0 4 1
A findOneBy() 0 4 1
A getClassName() 0 4 1
1
<?php
2
 
3
namespace JhFlexiTime\Repository;
4
 
5
use Doctrine\Common\Persistence\ObjectRepository;
6
use ZfcUser\Entity\UserInterface;
7
use JhFlexiTime\Entity\Booking;
8
use JhFlexiTime\DateTime\DateTime;
9
10
/**
11
 * Booking repository
12
 * 
13
 * @author Ben Lill <[email protected]>
14
 */
15
class BookingRepository implements BookingRepositoryInterface, ObjectRepository
16
{
17
 
18
    /**
19
     * @var \Doctrine\Common\Persistence\ObjectRepository
20
     */
21
    protected $bookingRepository;
22
23
    /**
24
     * @param ObjectRepository $bookingRepository
25
     */
26
    public function __construct(ObjectRepository $bookingRepository)
27
    {
28
        $this->bookingRepository = $bookingRepository;
29
    }
30
31
    /**
32
     * @param UserInterface $user
33
     * @return array
34
     */
35
    public function findAllByUser(UserInterface $user)
36
    {
37
        return $this->bookingRepository->findBy(
38
            ['user' => $user],
39
            ['date' => 'ASC'],
40
            null,
41
            null
42
        );
43
    }
44
    
45
    /**
46
     * Find all bookings for a given user/month
47
     *
48
     * @param \ZfcUser\Entity\UserInterface $user
49
     * @param DateTime $date
50
     * @return Booking[]
51
     */
52
    public function findByUserAndMonth(UserInterface $user, DateTime $date)
53
    {
54
        $firstDay = new DateTime(sprintf('first day of %s', $date->format('F Y')));
55
        $lastDay = new DateTime(sprintf('last day of %s', $date->format('F Y')));
56
        
57
        $params = [
58
            'user'      => $user,
59
            'firstDay'  => $firstDay->format('Y-m-d'),
60
            'lastDay'   => $lastDay->format('Y-m-d')
61
        ];
62
        
63
        $qb = $this->bookingRepository->createQueryBuilder('b');
64
        $qb->select('b')
65
            ->where('b.user = :user')
66
            ->andWhere('b.date >= :firstDay')
67
            ->andWhere('b.date <= :lastDay')
68
            ->setParameters($params)
69
            ->orderBy('b.date', 'ASC');
70
71
        return $qb->getQuery()->getResult();
72
    }
73
74
    /**
75
     * Check if this booking, is the first booking
76
     * for this user + month
77
     *
78
     * @param UserInterface $user
79
     * @param DateTime $month
80
     * @return bool
81
     */
82
    public function isUsersFirstBookingForMonth(UserInterface $user, DateTime $month)
83
    {
84
        $firstDay   = new DateTime(sprintf('first day of %s', $month->format('F Y')));
85
        $lastDay    = new DateTime(sprintf('last day of %s', $month->format('F Y')));
86
87
        $params = [
88
            'user'          => $user,
89
            'firstDay'      => $firstDay->format('Y-m-d'),
90
            'lastDay'       => $lastDay->format('Y-m-d')
91
        ];
92
93
        $qb = $this->bookingRepository->createQueryBuilder('b');
94
        $qb->select('b')
95
            ->where('b.user = :user')
96
            ->andWhere('b.date >= :firstDay')
97
            ->andWhere('b.date <= :lastDay')
98
            ->setParameters($params)
99
            ->setMaxResults(1);
100
101
        $result = $qb->getQuery()->getOneOrNullResult();
102
        //if result not an instance of Booking then
103
        //this is the first booking of the month
104
        return !$result instanceof Booking;
105
    }
106
107
    /**
108
     * Get the sum of all hours booked between the first day of the given month,
109
     * and the current day of the passed in DateTime object
110
     *
111
     * @param UserInterface $user
112
     * @param DateTime $date
113
     * @return float
114
     */
115
    public function getMonthBookedToDateTotalByUser(UserInterface $user, DateTime $date)
116
    {
117
        $firstDay = new DateTime(sprintf('first day of %s', $date->format('F Y')));
118
        $currentDay = new DateTime($date->format('Y-m-d'));
119
120
        $params = [
121
            'user'          => $user,
122
            'firstDay'      => $firstDay->format('Y-m-d'),
123
            'currentDay'    => $currentDay->format('Y-m-d')
124
        ];
125
126
        $qb = $this->bookingRepository->createQueryBuilder('b');
127
        $qb->select('sum(b.total)')
128
            ->where('b.user = :user')
129
            ->andWhere('b.date >= :firstDay')
130
            ->andWhere('b.date <= :currentDay')
131
            ->setParameters($params)
132
            ->orderBy('b.date', 'ASC');
133
134
        $totalHoursBookedThisMonth = $qb->getQuery()->getSingleScalarResult();
135
136
        if (null === $totalHoursBookedThisMonth) {
137
            $totalHoursBookedThisMonth = 0;
138
        }
139
140
        return $totalHoursBookedThisMonth;
141
    }
142
143
    /**
144
     * Get the sum of all all hours booked between the first day of the given month,
145
     * and the last day of the month
146
     *
147
     * @param UserInterface $user
148
     * @param DateTime $date
149
     * @return float
150
     */
151
    public function getMonthBookedTotalByUser(UserInterface $user, DateTime $date)
152
    {
153
        $firstDay   = new DateTime(sprintf('first day of %s', $date->format('F Y')));
154
        $lastDay    = new DateTime(sprintf('last day of %s', $date->format('F Y')));
155
156
        $params = [
157
            'user'      => $user,
158
            'firstDay'  => $firstDay->format('Y-m-d'),
159
            'lastDay'   => $lastDay->format('Y-m-d')
160
        ];
161
162
        $qb = $this->bookingRepository->createQueryBuilder('b');
163
        $qb->select('sum(b.total)')
164
            ->where('b.user = :user')
165
            ->andWhere('b.date >= :firstDay')
166
            ->andWhere('b.date <= :lastDay')
167
            ->setParameters($params)
168
            ->orderBy('b.date', 'ASC');
169
170
        $totalHoursBookedThisMonth = $qb->getQuery()->getSingleScalarResult();
171
172
        if (null === $totalHoursBookedThisMonth) {
173
            $totalHoursBookedThisMonth = 0;
174
        }
175
176
        return $totalHoursBookedThisMonth;
177
    }
178
179
    /**
180
     * @param UserInterface $user
181
     * @param DateTime $startDate
182
     * @param DateTime $endDate
183
     * @return float
184
     */
185
    public function getTotalBookedBetweenByUser(UserInterface $user, DateTime $startDate, DateTime $endDate)
186
    {
187
        $params = [
188
            'user'      => $user,
189
            'firstDay'  => $startDate->format('Y-m-d'),
190
            'lastDay'   => $endDate->format('Y-m-d')
191
        ];
192
193
        $qb = $this->bookingRepository->createQueryBuilder('b');
194
        $qb->select('sum(b.total)')
195
            ->where('b.user = :user')
196
            ->andWhere('b.date >= :firstDay')
197
            ->andWhere('b.date <= :lastDay')
198
            ->setParameters($params)
199
            ->orderBy('b.date', 'ASC');
200
201
        $totalHoursBookedThisPeriod = $qb->getQuery()->getSingleScalarResult();
202
203
        if (null === $totalHoursBookedThisPeriod) {
204
            $totalHoursBookedThisPeriod = 0;
205
        }
206
207
        return $totalHoursBookedThisPeriod;
208
    }
209
210
    /**
211
     * find(): defined by ObjectRepository.
212
     *
213
     * @see    ObjectRepository::find()
214
     * @param  int $id
215
     * @return Booking|null
216
     */
217
    public function find($id)
218
    {
219
        return $this->bookingRepository->find($id);
220
    }
221
222
    /**
223
     * findAll(): defined by ObjectRepository.
224
     *
225
     * @see    ObjectRepository::findAll()
226
     * @return Booking[]
227
     */
228
    public function findAll()
229
    {
230
        return $this->bookingRepository->findAll();
231
    }
232
233
    /**
234
     * findBy(): defined by ObjectRepository.
235
     *
236
     * @see    ObjectRepository::findBy()
237
     * @param  array      $criteria
238
     * @param  array|null $orderBy
239
     * @param  int|null   $limit
240
     * @param  int|null   $offset
241
     * @return Booking[]
242
     */
243
    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
244
    {
245
        return $this->bookingRepository->findBy($criteria, $orderBy, $limit, $offset);
246
    }
247
248
    /**
249
     * findOneBy(): defined by ObjectRepository.
250
     *
251
     * @see    ObjectRepository::findOneBy()
252
     * @param  array $criteria
253
     * @return Booking|null
254
     */
255
    public function findOneBy(array $criteria)
256
    {
257
        return $this->bookingRepository->findOneBy($criteria);
258
    }
259
260
    /**
261
     * getClassName(): defined by ObjectRepository.
262
     *
263
     * @see    ObjectRepository::getClassName()
264
     * @return string
265
     */
266
    public function getClassName()
267
    {
268
        return $this->bookingRepository->getClassName();
269
    }
270
}
271