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

ElfinderController::getServiceLocator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
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 5
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace PlaygroundCore\Controller\Admin;
3
4
use Zend\Mvc\Controller\AbstractActionController;
5
use Zend\View\Model\ViewModel;
6
use Zend\ServiceManager\ServiceLocatorInterface;
7
8
class ElfinderController extends AbstractActionController
9
{
10
11
    protected $Config;
12
13
    /**
14
     *
15
     * @var ServiceManager
16
     */
17
    protected $serviceLocator;
18
19
    public function __construct(ServiceLocatorInterface $locator)
20
    {
21
        $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...
22
    }
23
24
    public function getServiceLocator()
25
    {
26
        
27
        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...
28
    }
29
30
    /**
31
     * @return array|\Zend\View\Model\ViewModel
32
     */
33 View Code Duplication
    public function indexAction()
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...
34
    {
35
        $view = new ViewModel();
36
        $this->getConfig();
37
        $view->BasePath = $this->Config['BasePath'];
38
        $view->ConnectorPath = '/admin/elfinder/connector';
39
40
        return $view;
41
    }
42
43
    /**
44
     * @return \Zend\View\Model\ViewModel
45
     */
46 View Code Duplication
    public function ckeditorAction()
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...
47
    {
48
        $view = new ViewModel();
49
        $this->getConfig();
50
        $view->BasePath    = $this->Config['BasePath'];
51
        $view->ConnectorPath = '/admin/elfinder/connector';
52
        $view->setTerminal(true);
53
54
        return $view;
55
    }
56
57
    /**
58
     * @return \Zend\View\Model\ViewModel
59
     */
60
    public function connectorAction()
61
    {
62
        $view = new ViewModel();
63
        $this->getConfig();
64
65
        $root = array();
66
        if (isset($this->Config['QuRoots'])) {
67
            $root = $this->Config['QuRoots'];
68
        }
69
70
        $opts = array(
71
            'debug' => false,
72
            'roots' => array($root)
73
        );
74
75
        $connector = new \elFinderConnector(new \elFinder($opts));
76
        $connector->run();
77
78
        $view->setTerminal(true);
79
80
        return $view;
81
    }
82
83
    /**
84
     * @param $attr
85
     * @param $path
86
     * @param $data
87
     * @param $volume
88
     *
89
     * @return bool|null
90
     */
91
    public function access($attr, $path, $data, $volume)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $volume is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
    {
93
        return strpos(basename($path), '.') === 0
94
            ? !($attr == 'read' || $attr == 'write')
95
            :  null;
96
    }
97
98
    /**
99
     * @return mixed
100
     */
101
    public function getConfig()
102
    {
103
        if (!$this->Config) {
104
            $config       = $this->getServiceLocator()->get('config');
105
            if (isset($config['playgroundcore']) && isset($config['playgroundcore']['QuConfig']) && isset($config['playgroundcore']['QuConfig']['QuElFinder'])) {
106
                $this->Config = $config['playgroundcore']['QuConfig']['QuElFinder'];
107
            } else {
108
                $this->Config = array();
109
            }
110
        }
111
112
        return $this->Config;
113
    }
114
}
115