BookingRestController::getList()   B
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
rs 8.5806
cc 4
eloc 25
nc 3
nop 0
1
<?php
2
3
namespace JhFlexiTime\Controller;
4
5
use JhFlexiTime\Repository\UserSettingsRepositoryInterface;
6
use JhUser\Repository\UserRepositoryInterface;
7
use Zend\Mvc\Controller\AbstractRestfulController;
8
use Zend\Validator\Date as DateValidator;
9
use Zend\View\Model\JsonModel;
10
use JhFlexiTime\Service\TimeCalculatorService;
11
use JhFlexiTime\Service\BookingService;
12
13
/**
14
 * Class BookingRestController
15
 * @package JhFlexiTime\Controller
16
 * @author Aydin Hassan <[email protected]>
17
 */
18
class BookingRestController extends AbstractRestfulController
19
{
20
    use GetSetDateTrait;
21
22
    /**
23
     * @var \JhFlexiTime\Service\BookingService
24
     */
25
    protected $bookingService;
26
27
    /**
28
     * @var \JhFlexiTime\Service\TimeCalculatorService
29
     */
30
    protected $timeCalculatorService;
31
32
    /**
33
     * @var UserRepositoryInterface
34
     */
35
    protected $userRepository;
36
37
    /**
38
     * @var UserSettingsRepositoryInterface
39
     */
40
    protected $userSettingsRepository;
41
42
    /**
43
     * @param BookingService $bookingService
44
     * @param TimeCalculatorService $timeCalculatorService
45
     * @param UserRepositoryInterface $userRepository
46
     * @param UserSettingsRepositoryInterface $userSettingsRepository
47
     */
48
    public function __construct(
49
        BookingService $bookingService,
50
        TimeCalculatorService $timeCalculatorService,
51
        UserRepositoryInterface $userRepository,
52
        UserSettingsRepositoryInterface $userSettingsRepository
53
    ) {
54
        $this->bookingService           = $bookingService;
55
        $this->timeCalculatorService    = $timeCalculatorService;
56
        $this->userRepository           = $userRepository;
57
        $this->userSettingsRepository   = $userSettingsRepository;
58
    }
59
60
    /**
61
     * @return JsonModel
62
     */
63
    public function getList()
64
    {
65
        $month  = $this->params()->fromQuery('m', false);
66
        $year   = $this->params()->fromQuery('y', false);
67
        $userId = $this->params()->fromQuery('user', false);
68
        $period = $this->getDate($month, $year);
69
70
        if ($userId && $this->isGranted('flexi-time.readOthers')) {
0 ignored issues
show
Documentation Bug introduced by
The method isGranted does not exist on object<JhFlexiTime\Contr...\BookingRestController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
71
72
            $user = $this->userRepository->find($userId);
73
            if (!$user) {
74
                return new JsonModel([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Zend\View\Mo...User does not exist')); (Zend\View\Model\JsonModel) is incompatible with the return type of the parent method Zend\Mvc\Controller\Abst...tfulController::getList of type array<string,string>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
75
                    'success' => false,
76
                    'message' => 'User does not exist',
77
                ]);
78
            }
79
        } else {
80
            $user = $this->zfcUserAuthentication()->getIdentity();
0 ignored issues
show
Documentation Bug introduced by
The method zfcUserAuthentication does not exist on object<JhFlexiTime\Contr...\BookingRestController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
81
        }
82
83
        $userSettings   = $this->userSettingsRepository->findOneByUser($user);
84
        $records        = $this->bookingService->getUserBookingsForMonth($user, $period);
85
        $pagination     = $this->bookingService->getPagination($period);
86
        $totals         = $this->timeCalculatorService->getTotals($user, $userSettings->getFlexStartDate(), $period);
87
88
        return new JsonModel([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Zend\View\Mo...w \DateTime('today'))); (Zend\View\Model\JsonModel) is incompatible with the return type of the parent method Zend\Mvc\Controller\Abst...tfulController::getList of type array<string,string>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
89
            'bookings' => [
90
                'records'   => $records,
91
                'totals'    => $totals,
92
                'user'      => $user,
93
            ],
94
            'pagination' => $pagination,
95
            'date'       => $period,
96
            'today'      => new \DateTime("today"),
97
        ]);
98
    }
99
100
    /**
101
     * @param int $id
102
     * @return JsonModel
103
     */
104
    public function get($id)
105
    {
106
        $id   = $this->parseIdCriteria($id);
107
        return new JsonModel([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Zend\View\Mo...user'], $id['date']))); (Zend\View\Model\JsonModel) is incompatible with the return type of the parent method Zend\Mvc\Controller\AbstractRestfulController::get of type array<string,string>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
108
            'booking' => $this->bookingService->getBookingByUserAndDate($id['user'], $id['date']),
109
        ]);
110
    }
111
112
    /**
113
     * @param array $data
114
     * @return JsonModel
115
     */
116
    public function create($data)
117
    {
118
        $booking = $this->bookingService->create($data);
119
120
        if (is_array($booking)) {
121
            $booking['success'] = false;
122
            return new JsonModel($booking);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Zend\View\Model\JsonModel($booking); (Zend\View\Model\JsonModel) is incompatible with the return type of the parent method Zend\Mvc\Controller\Abst...stfulController::create of type array<string,string>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
123
        }
124
125
        $userSettings = $this->userSettingsRepository->findOneByUser($booking->getUser());
126
        $monthTotals  = $this->timeCalculatorService->getTotals(
127
            $booking->getUser(),
128
            $userSettings->getFlexStartDate(),
129
            $booking->getDate()
130
        );
131
        return new JsonModel([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Zend\View\Mo...$booking->getDate()))); (Zend\View\Model\JsonModel) is incompatible with the return type of the parent method Zend\Mvc\Controller\Abst...stfulController::create of type array<string,string>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
132
            'success'       => true,
133
            'booking'       => $booking,
134
            'monthTotals'   => $monthTotals,
135
            'weekTotals'    => $this->timeCalculatorService->getWeekTotals($booking->getUser(), $booking->getDate())
136
        ]);
137
    }
138
139
    /**
140
     * @param int $id
141
     * @param array $data
142
     * @return JsonModel
143
     */
144
    public function update($id, $data)
145
    {
146
        $id         = $this->parseIdCriteria($id);
147
        $booking    = $this->bookingService->update($id['user'], $id['date'], $data);
148
149
        if (is_array($booking)) {
150
            $booking['success'] = false;
151
            return new JsonModel($booking);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Zend\View\Model\JsonModel($booking); (Zend\View\Model\JsonModel) is incompatible with the return type of the parent method Zend\Mvc\Controller\Abst...stfulController::update of type array<string,string>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
152
        }
153
154
        $userSettings = $this->userSettingsRepository->findOneByUser($booking->getUser());
155
        $monthTotals  = $this->timeCalculatorService->getTotals(
156
            $booking->getUser(),
157
            $userSettings->getFlexStartDate(),
158
            $booking->getDate()
159
        );
160
        return new JsonModel([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Zend\View\Mo...$booking->getDate()))); (Zend\View\Model\JsonModel) is incompatible with the return type of the parent method Zend\Mvc\Controller\Abst...stfulController::update of type array<string,string>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
161
            'success'       => true,
162
            'booking'       => $booking,
163
            'monthTotals'   => $monthTotals,
164
            'weekTotals'    => $this->timeCalculatorService->getWeekTotals($booking->getUser(), $booking->getDate())
165
        ]);
166
    }
167
168
    /**
169
     * @param int $id
170
     * @return JsonModel
171
     */
172
    public function delete($id)
173
    {
174
        $id         = $this->parseIdCriteria($id);
175
        $booking    = $this->bookingService->delete($id['user'], $id['date']);
176
        if (is_array($booking)) {
177
            $booking['success'] = false;
178
            return new JsonModel($booking);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Zend\View\Model\JsonModel($booking); (Zend\View\Model\JsonModel) is incompatible with the return type of the parent method Zend\Mvc\Controller\Abst...stfulController::delete of type array<string,string>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
179
        }
180
181
        $userSettings = $this->userSettingsRepository->findOneByUser($booking->getUser());
182
        $monthTotals  = $this->timeCalculatorService->getTotals(
183
            $booking->getUser(),
184
            $userSettings->getFlexStartDate(),
185
            $booking->getDate()
186
        );
187
        return new JsonModel([
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Zend\View\Mo...$booking->getDate()))); (Zend\View\Model\JsonModel) is incompatible with the return type of the parent method Zend\Mvc\Controller\Abst...stfulController::delete of type array<string,string>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
188
            'success'       => true,
189
            'monthTotals'   => $monthTotals,
190
            'weekTotals'    => $this->timeCalculatorService->getWeekTotals($booking->getUser(), $booking->getDate())
191
        ]);
192
    }
193
194
    /**
195
     * @param $id
196
     * @return array
197
     */
198
    public function parseIdCriteria($id)
199
    {
200
        $idParts    = explode("-", $id);
201
        $date       = new \JhFlexiTime\DateTime\DateTime();
202
        $date->setTimestamp($idParts[0]);
203
        return ['date' => $date,'user' => $idParts[1]];
204
    }
205
}
206