VideoController::save()   A
last analyzed

Complexity

Conditions 4
Paths 14

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 9.568
c 0
b 0
f 0
cc 4
nc 14
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\VideoService;
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 VideoController 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
    /** @var Template
21
     */
22
    private $template;
23
24
    /** @var VideoService */
25
    private $videoService;
26
27
    /** @var SessionManager */
28
    private $session;
29
30
    /** @var Router */
31
    private $router;
32
33
    /** @var CategoryService */
34
    private $categoryService;
35
36
    /**
37
     * VideoController constructor.
38
     *
39
     * @param Template        $template
40
     * @param Router          $router
41
     * @param VideoService    $videoService
42
     * @param SessionManager  $session
43
     * @param CategoryService $categoryService
44
     */
45
    public function __construct(
46
        Template $template,
47
        Router $router,
48
        VideoService $videoService,
49
        SessionManager $session,
50
        CategoryService $categoryService
51
    ) {
52
        $this->template = $template;
53
        $this->videoService = $videoService;
54
        $this->session = $session;
55
        $this->router = $router;
56
        $this->categoryService = $categoryService;
57
    }
58
59
    public function index(): HtmlResponse
60
    {
61
        $params = $this->request->getQueryParams();
62
        $page = isset($params['page']) ? $params['page'] : 1;
63
        $limit = isset($params['limit']) ? $params['limit'] : 15;
64
        $videos = $this->videoService->fetchAllArticles($page, $limit);
65
66
        return new HtmlResponse($this->template->render('article::video/index',
67
            ['list' => $videos, 'layout' => 'layout/admin'])
68
        );
69
    }
70
71
    /**
72
     * Add/Edit show form.
73
     *
74
     * @return \Psr\Http\Message\ResponseInterface
75
     */
76
    public function edit($errors = []): \Psr\Http\Message\ResponseInterface
77
    {
78
        $id = $this->request->getAttribute('id');
79
        $video = $this->videoService->fetchSingleArticle($id);
80
        $categories = $this->categoryService->getAll(ArticleType::VIDEO);
81
82
        if ($this->request->getParsedBody()) {
83
            $video = (object) ($this->request->getParsedBody() + (array) $video);
84
            $video->article_id = $id;
85
        }
86
87
        return new HtmlResponse(
88
            $this->template->render(
89
                'article::video/edit', [
90
                    'video'      => $video,
91
                    'categories' => $categories,
92
                    'errors'     => $errors,
93
                    'layout'     => 'layout/admin',
94
                ]
95
            )
96
        );
97
    }
98
99
    /**
100
     * Add/Edit article action.
101
     *
102
     * @throws FilterException if filter fails
103
     * @throws \Exception
104
     *
105
     * @return \Psr\Http\Message\ResponseInterface
106
     */
107
    public function save(): \Psr\Http\Message\ResponseInterface
108
    {
109
        try {
110
            $id = $this->request->getAttribute('id');
111
            $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...
112
            $data = $this->request->getParsedBody();
113
            $data += (new Request())->getFiles()->toArray();
114
115
            if ($id) {
116
                $this->videoService->updateArticle($data, $id);
117
            } else {
118
                $this->videoService->createArticle($user, $data);
119
            }
120
        } catch (FilterException $fe) {
121
            return $this->edit($fe->getArrayMessages());
122
        } catch (\Exception $e) {
123
            throw $e;
124
        }
125
126
        return $this->response->withStatus(302)->withHeader('Location',
127
            $this->router->generateUri('admin.videos'));
128
    }
129
130
    /**
131
     * Delete video by id.
132
     *
133
     * @throws \Exception
134
     *
135
     * @return \Psr\Http\Message\ResponseInterface
136
     */
137
    public function delete(): \Psr\Http\Message\ResponseInterface
138
    {
139
        try {
140
            $this->videoService->deleteArticle($this->request->getAttribute('id'));
141
        } catch (\Exception $e) {
142
            throw $e;
143
        }
144
145
        return $this->response->withStatus(302)->withHeader('Location',
146
            $this->router->generateUri('admin.videos', ['action' => 'index'])
147
        );
148
    }
149
}
150