EventController::index()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Article\Controller;
6
7
use Article\Entity\ArticleType;
8
use Article\Service\EventService;
9
use Category\Service\CategoryService;
10
use Std\AbstractController;
11
use Std\FilterException;
12
use Zend\Diactoros\Response\HtmlResponse;
13
use Zend\Expressive\Router\RouterInterface as Router;
14
use Zend\Expressive\Template\TemplateRendererInterface as Template;
15
use Zend\Http\PhpEnvironment\Request;
16
use Zend\Session\SessionManager;
17
18 View Code Duplication
class EventController extends AbstractController
0 ignored issues
show
Duplication introduced by
This class 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...
19
{
20
    private $template;
21
    private $router;
22
    private $eventService;
23
    private $session;
24
    private $categoryService;
25
26
    /**
27
     * EventController constructor.
28
     *
29
     * @param Template        $template
30
     * @param Router          $router
31
     * @param EventService    $eventService
32
     * @param SessionManager  $session
33
     * @param CategoryService $categoryService
34
     */
35
    public function __construct(
36
        Template $template,
37
        Router $router,
38
        EventService $eventService,
39
        SessionManager $session,
40
        CategoryService $categoryService
41
    ) {
42
        $this->template = $template;
43
        $this->router = $router;
44
        $this->eventService = $eventService;
45
        $this->session = $session;
46
        $this->categoryService = $categoryService;
47
    }
48
49
    public function index(): \Psr\Http\Message\ResponseInterface
50
    {
51
        $params = $this->request->getQueryParams();
52
        $page = isset($params['page']) ? $params['page'] : 1;
53
        $limit = isset($params['limit']) ? $params['limit'] : 15;
54
        $events = $this->eventService->fetchAllArticles($page, $limit);
55
56
        return new HtmlResponse($this->template->render(
57
            'article::event/index',
58
            ['list' => $events, 'layout' => 'layout/admin'])
59
        );
60
    }
61
62
    public function edit($errors = []): \Psr\Http\Message\ResponseInterface
63
    {
64
        $id = $this->request->getAttribute('id');
65
        $event = $this->eventService->fetchSingleArticle($id);
66
        $categories = $this->categoryService->getAll(ArticleType::EVENT);
67
68
        if ($this->request->getParsedBody()) {
69
            $event = (object) ($this->request->getParsedBody() + (array) $event);
70
            $event->article_id = $id;
71
        }
72
73
        return new HtmlResponse(
74
            $this->template->render(
75
                'article::event/edit', [
76
                    'event'      => $event,
77
                    'categories' => $categories,
78
                    'errors'     => $errors,
79
                    'layout'     => 'layout/admin',
80
                ]
81
            )
82
        );
83
    }
84
85
    public function save()
86
    {
87
        try {
88
            $id = $this->request->getAttribute('id');
89
            $user = $this->session->getStorage()->user;
0 ignored issues
show
Bug introduced by
Accessing user on the interface Zend\Session\Storage\StorageInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
90
            $data = $this->request->getParsedBody();
91
            $data += (new Request())->getFiles()->toArray();
92
93
            if ($id) {
94
                $this->eventService->updateArticle($data, $id);
95
            } else {
96
                $this->eventService->createArticle($user, $data);
97
            }
98
        } catch (FilterException $fe) {
99
            return $this->edit($fe->getArrayMessages());
100
        } catch (\Exception $e) {
101
            throw $e;
102
        }
103
104
        return $this->response->withStatus(302)->withHeader('Location', $this->router->generateUri('admin.events'));
105
    }
106
107
    public function delete()
108
    {
109
        try {
110
            $this->eventService->deleteArticle($this->request->getAttribute('id'));
111
        } catch (\Exception $e) {
112
            throw $e;
113
        }
114
115
        return $this->response->withStatus(302)->withHeader(
116
            'Location', $this->router->generateUri('admin.events')
117
        );
118
    }
119
}
120