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

WebsiteController::indexAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Zend Framework (http://framework.zend.com/)
4
 *
5
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
6
 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7
 * @license   http://framework.zend.com/license/new-bsd New BSD License
8
 */
9
10
namespace PlaygroundCore\Controller\Admin;
11
12
use Zend\Mvc\Controller\AbstractActionController;
13
use Zend\View\Model\ViewModel;
14
use Zend\ServiceManager\ServiceLocatorInterface;
15
16
class WebsiteController extends AbstractActionController
17
{
18
19
    protected $websiteService;
20
21
    protected $localeService;
22
23
    /**
24
     *
25
     * @var ServiceManager
26
     */
27
    protected $serviceLocator;
28
29
    public function __construct(ServiceLocatorInterface $locator)
30
    {
31
        $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\Co...r\Admin\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...
32
    }
33
34
    public function getServiceLocator()
35
    {
36
        
37
        return $this->serviceLocator;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->serviceLocator; (PlaygroundCore\Controller\Admin\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...
38
    }
39
40
    public function indexAction()
41
    {
42
        return new ViewModel();
43
    }
44
45
    public function listAction()
46
    {
47
        $locales = $this->getLocaleService()->getLocaleMapper()->findBy(array('active_front' => 1));
48
        $user = $this->zfcUserAuthentication()->getIdentity();
0 ignored issues
show
Documentation Bug introduced by
The method zfcUserAuthentication does not exist on object<PlaygroundCore\Co...dmin\WebsiteController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
49
50
        $websites = $this->getWebsiteService()->getWebsiteMapper()->findAll();
51
52
        return new ViewModel(compact("websites", "locales", "user"));
53
    }
54
55
    public function editActiveAction()
56
    {
57
        $websiteId = $this->getEvent()->getRouteMatch()->getParam('websiteId');
58
        $website = $this->getWebsiteService()->getWebsiteMapper()->findBy(array('id' => $websiteId));
59
        $website = $website[0];
60
61
        if ($website->getDefault()) {
62
            return $this->redirect()->toRoute('admin');
63
        }
64
        $website->setActive(!$website->getActive());
65
        $this->getWebsiteService()->getWebsiteMapper()->update($website);
66
67
        return $this->redirect()->toRoute('admin');
68
    }
69
70 View Code Duplication
    public function getWebsiteService()
0 ignored issues
show
Duplication introduced by
This method 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...
71
    {
72
        if (null === $this->websiteService) {
73
            $this->websiteService = $this->getServiceLocator()->get('playgroundcore_website_service');
74
        }
75
76
        return $this->websiteService;
77
    }
78
79
    public function setWebsiteService($websiteService)
80
    {
81
        $this->websiteService = $websiteService;
82
83
        return $this;
84
    }
85
86 View Code Duplication
    public function getLocaleService()
0 ignored issues
show
Duplication introduced by
This method 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...
87
    {
88
        if (null === $this->localeService) {
89
            $this->localeService = $this->getServiceLocator()->get('playgroundcore_locale_service');
90
        }
91
92
        return $this->localeService;
93
    }
94
95
    public function setLocaleService($localeService)
96
    {
97
        $this->localeService = $localeService;
98
99
        return $this;
100
    }
101
}
102