Completed
Pull Request — master (#39)
by
unknown
02:27
created

ArticleController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 11
dl 0
loc 110
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A index() 0 11 1
B create() 0 41 5
A doCreate() 0 5 1
1
<?php
2
3
namespace Admin\Controller;
4
5
use Zend\Expressive\Template\TemplateRendererInterface as Template;
6
use Zend\Diactoros\Response\HtmlResponse;
7
use Admin\Model\Repository\ArticleRepositoryInterface;
8
use Admin\Validator\ValidatorInterface as Validator;
9
use Ramsey\Uuid\Uuid;
10
use Zend\Session\SessionManager;
11
use Zend\Expressive\Router\RouterInterface as Router;
12
13
class ArticleController extends AbstractController
14
{
15
    /**
16
     * @var Template
17
     */
18
    private $template;
19
20
    /**
21
     * @var \Admin\Model\Repository\ArticleRepositoryInterface
22
     */
23
    private $articleRepo;
24
25
    /**
26
     * @var \Zend\Validator\AbstractValidator
27
     */
28
    private $validator;
29
30
    /**
31
     * @var SessionManager
32
     */
33
    private $session;
34
35
    /**
36
     * @var ROUTER
37
     */
38
    private $router;
39
40
    /**
41
     * ArticleController constructor.
42
     *
43
     * @param Template $template
44
     * @param ArticleRepositoryInterface $articleRepo
45
     * @param Validator $validator
46
     * @param SessionManager $session
47
     * @param Router $routes
0 ignored issues
show
Bug introduced by
There is no parameter named $routes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
48
     */
49
    public function __construct(
50
        Template $template,
51
        ArticleRepositoryInterface $articleRepo,
52
        Validator $validator,
53
        SessionManager $session,
54
        Router $router
55
    ) {
56
        $this->template = $template;
57
        $this->articleRepo = $articleRepo;
58
        $this->validator = $validator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $validator of type object<Admin\Validator\ValidatorInterface> is incompatible with the declared type object<Zend\Validator\AbstractValidator> of property $validator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
        $this->session = $session;
60
        $this->router = $router;
0 ignored issues
show
Documentation Bug introduced by
It seems like $router of type object<Zend\Expressive\Router\RouterInterface> is incompatible with the declared type object<Admin\Controller\ROUTER> of property $router.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
    }
62
63
    public function index() : HtmlResponse
64
    {
65
        $articleCollection = $this->articleRepo->fetchAllArticles();
66
67
        $data = [
68
            'message'    => 'Article list',
69
            'articleCollection' => $articleCollection,
70
        ];
71
72
        return new HtmlResponse($this->template->render('admin::article/index', $data));
73
    }
74
75
    public function create() : \Psr\Http\Message\ResponseInterface
76
    {
77
        $data = [
78
            'message' => 'Create article',
79
            'errors' => false,
80
            'data' => $this->request->getParsedBody()
81
        ];
82
83
        if (count($data['data']) > 0) {
84
            $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...
85
            try {
86
                $article = new \Admin\Model\Entity\ArticleEntity();
87
                //@TODO fix this
88
                $dt = new \DateTime('now');
89
                $data['data']['created_at'] = $dt->format('Y-m-d H:i:s');
90
91
                //generate uuid
92
                $data['data']['article_uuid'] = hex2bin(Uuid::uuid1()->getHex());
93
                $data['user_uuid'] = $user->admin_user_uuid;
94
                $this->validator->validate($data['data']);
0 ignored issues
show
Bug introduced by
The method validate() does not seem to exist on object<Zend\Validator\AbstractValidator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
                $article->exchangeArray($data['data']);
96
                if ($this->articleRepo->createArticle($article)) {
97
                    return $this->response->withStatus(302)->withHeader(
98
                        'Location',
99
                        $this->router->generateUri('admin.articles', ['action' => 'index'])
100
                    );
101
                }
102
                //@TODO there was an error saving article, set a flesh message for user
103
104
                // handle validation errors
105
            } catch (\Admin\Validator\ValidatorException $e) {
106
                $data['errors'] = $this->validator->getMessages();
107
108
                // handle other errors
109
            } catch (\Exception $e) {
110
                var_dump($e->getMessage());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($e->getMessage()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
111
            }
112
        }
113
114
        return new HtmlResponse($this->template->render('admin::article/create', $data));
115
    }
116
117
    public function doCreate()
118
    {
119
120
121
    }
122
}