Completed
Push — develop ( e37f14...da7aa8 )
by greg
03:12
created

ConsoleController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 8
c 4
b 2
f 0
lcom 1
cbo 2
dl 0
loc 62
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getServiceLocator() 0 5 1
A cronAction() 0 21 3
A getCronService() 0 8 2
A setCronService() 0 6 1
1
<?php
2
3
namespace PlaygroundCore\Controller;
4
5
use Zend\Mvc\Controller\AbstractActionController;
6
use Zend\Console\Response as ConsoleResponse;
7
use PlaygroundCore\Service\Cron;
8
use Zend\ServiceManager\ServiceLocatorInterface;
9
10
class ConsoleController extends AbstractActionController
11
{
12
    /**
13
     *
14
     * @var ServiceManager
15
     */
16
    protected $serviceLocator;
17
18
    public function __construct(ServiceLocatorInterface $locator)
19
    {
20
        $this->serviceLocator = $locator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $locator of type object<Zend\ServiceManag...erviceLocatorInterface> is incompatible with the declared type object<PlaygroundCore\Controller\ServiceManager> of property $serviceLocator.

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...
21
    }
22
23
    public function getServiceLocator()
24
    {
25
        
26
        return $this->serviceLocator;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->serviceLocator; (PlaygroundCore\Controller\ServiceManager) is incompatible with the return type of the parent method Zend\Mvc\Controller\Abst...ller::getServiceLocator of type Zend\ServiceManager\ServiceLocatorInterface.

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...
27
    }
28
29
    /**
30
     * @var cronService
31
     */
32
    protected $cronService;
33
34
    public function cronAction()
35
    {
36
        $response = $this->getResponse();
37
38
        $cronjobs = $this->getCronService();
39
40
        $cronjobs->getCronjobs();
41
42
        if (count($cronjobs) > 0) {
43
            $cronjobs->run();
44
        }
45
46
        if (!$response instanceof ConsoleResponse) {
47
            $response->setStatusCode(200);
48
            $response->setContent('ok');
49
50
            return $response;
51
        } else {
52
            return;
53
        }
54
    }
55
56
    public function getCronService()
57
    {
58
        if (!$this->cronService) {
59
            $this->cronService = $this->getServiceLocator()->get('playgroundcore_cron_service');
60
        }
61
62
        return $this->cronService;
63
    }
64
65
    public function setCronService(Cron $cronService)
66
    {
67
        $this->cronService = $cronService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cronService of type object<PlaygroundCore\Service\Cron> is incompatible with the declared type object<PlaygroundCore\Controller\cronService> of property $cronService.

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...
68
69
        return $this;
70
    }
71
}
72