ReadController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 5
Bugs 0 Features 3
Metric Value
wmc 3
c 5
b 0
f 3
lcom 1
cbo 4
dl 0
loc 53
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A onDispatch() 0 14 2
1
<?php
2
3
namespace Sebaks\Crud\Controller;
4
5
use Zend\Mvc\MvcEvent;
6
use Zend\Mvc\Controller\AbstractActionController;
7
use T4webDomainInterface\Infrastructure\CriteriaInterface;
8
use T4webDomainInterface\Infrastructure\RepositoryInterface;
9
use Sebaks\Crud\View\Model\ReadViewModelInterface;
10
11
class ReadController extends AbstractActionController
12
{
13
    /**
14
     * @var CriteriaInterface
15
     */
16
    private $criteria;
17
18
    /**
19
     * @var RepositoryInterface
20
     */
21
    private $repository;
22
23
    /**
24
     * @var ReadViewModelInterface
25
     */
26
    private $viewModel;
27
28
    /**
29
     * @param CriteriaInterface $criteria
30
     * @param RepositoryInterface $repository
31
     * @param ReadViewModelInterface $viewModel
32
     */
33
    public function __construct(
34
        CriteriaInterface $criteria,
35
        RepositoryInterface $repository,
36
        ReadViewModelInterface $viewModel)
37
    {
38
        $this->criteria = $criteria;
39
        $this->repository = $repository;
40
        $this->viewModel = $viewModel;
41
    }
42
43
    /**
44
     * Execute the request
45
     *
46
     * @param  MvcEvent $e
47
     * @return ReadViewModelInterface
48
     */
49
    public function onDispatch(MvcEvent $e)
50
    {
51
        $entity = $this->repository->find($this->criteria);
52
53
        if (!$entity) {
54
            return $this->notFoundAction();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->notFoundAction(); (array) is incompatible with the return type documented by Sebaks\Crud\Controller\ReadController::onDispatch of type Sebaks\Crud\View\Model\ReadViewModelInterface.

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...
55
        }
56
57
        $this->viewModel->setEntity($entity);
58
59
        $e->setResult($this->viewModel);
60
61
        return $this->viewModel;
62
    }
63
}
64