Completed
Pull Request — develop (#542)
by Mathias
09:01
created

FileController::indexAction()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 36
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 36
c 0
b 0
f 0
ccs 17
cts 17
cp 1
rs 9.7333
cc 4
nc 5
nop 0
crap 4
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** FileController.php */
11
namespace Core\Controller;
12
13
use Core\EventManager\EventManager;
14
use Core\Listener\Events\FileEvent;
15
use Core\Repository\RepositoryService;
16
use Interop\Container\ContainerInterface;
17
use Organizations\Entity\OrganizationImage;
18
use Zend\Mvc\Controller\AbstractActionController;
19
use Zend\View\Model\JsonModel;
20
use Zend\Mvc\MvcEvent;
21
use Core\Entity\PermissionsInterface;
22
23
/**
24
 * Class FileController
25
 *
26
 * @method \Acl\Controller\Plugin\Acl acl()
27
 * @package Core\Controller
28
 */
29
class FileController extends AbstractActionController
30
{
31
    /**
32
     * @var RepositoryService
33
     */
34
    private $repositories;
35
    
36
    /**
37
     * @var EventManager
38
     */
39
    private $coreFileEvents;
40
    
41 6
    public function __construct(
42
        RepositoryService $repositories,
43
        EventManager $eventManager
44
    ) {
45 6
        $this->repositories = $repositories;
46 6
        $this->coreFileEvents = $eventManager;
47 6
    }
48
    
49
    
50 5
    protected function attachDefaultListeners()
51
    {
52 5
        parent::attachDefaultListeners();
53 5
        $events = $this->getEventManager();
54 5
        $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'preDispatch'), 10);
55 5
    }
56
57 5
    public function preDispatch(MvcEvent $e)
58
    {
59 5
        if ('delete' == $this->params()->fromQuery('do') && $this->getRequest()->isXmlHttpRequest()) {
0 ignored issues
show
Bug introduced by
The method isXmlHttpRequest() does not exist on Zend\Stdlib\RequestInterface. It seems like you code against a sub-type of Zend\Stdlib\RequestInterface such as Zend\Http\Request. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        if ('delete' == $this->params()->fromQuery('do') && $this->getRequest()->/** @scrutinizer ignore-call */ isXmlHttpRequest()) {
Loading history...
60 2
            $routeMatch = $e->getRouteMatch();
61 2
            $routeMatch->setParam('action', 'delete');
62
        }
63 5
    }
64
65
    /**
66
     * @return null|object
67
     */
68 5
    protected function getFile()
69
    {
70 5
        $fileStoreName = $this->params('filestore');
71 5
        list($module, $entityName) = explode('.', $fileStoreName);
0 ignored issues
show
Bug introduced by
It seems like $fileStoreName can also be of type Zend\Mvc\Controller\Plugin\Params; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        list($module, $entityName) = explode('.', /** @scrutinizer ignore-type */ $fileStoreName);
Loading history...
72 5
        $response      = $this->getResponse();
73
74
        try {
75 5
            $repository = $this->repositories->get($module . '/' . $entityName);
76 1
        } catch (\Exception $e) {
77 1
            $response->setStatusCode(404);
0 ignored issues
show
Bug introduced by
The method setStatusCode() does not exist on Zend\Stdlib\ResponseInterface. It seems like you code against a sub-type of Zend\Stdlib\ResponseInterface such as Zend\Http\Response. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            $response->/** @scrutinizer ignore-call */ 
78
                       setStatusCode(404);
Loading history...
78 1
            $this->getEvent()->setParam('exception', $e);
79 1
            return null;
80
        }
81 4
        $fileId = $this->params('fileId', 0);
82 4
        if (preg_match('/^(.*)\..*$/', $fileId, $baseFileName)) {
0 ignored issues
show
Bug introduced by
It seems like $fileId can also be of type Zend\Mvc\Controller\Plugin\Params; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        if (preg_match('/^(.*)\..*$/', /** @scrutinizer ignore-type */ $fileId, $baseFileName)) {
Loading history...
83 4
            $fileId = $baseFileName[1];
84
        }
85 4
        $file       = $repository->find($fileId);
86
                
87 4
        if (!$file) {
88 2
            $response->setStatusCode(404);
89
        }
90 4
        return $file;
91
    }
92
93
    /**
94
     * @return \Zend\Http\PhpEnvironment\Response
95
     */
96 3
    public function indexAction()
97
    {
98
        /* @var \Zend\Http\PhpEnvironment\Response $response */
99 3
        $response = $this->getResponse();
100
        /* @var \Core\Entity\FileEntity $file */
101 3
        $file     = $this->getFile();
102
        
103 3
        if (!$file) {
0 ignored issues
show
introduced by
$file is of type Core\Entity\FileEntity, thus it always evaluated to true.
Loading history...
104 2
            return $response;
105
        }
106
        
107 1
        $this->acl($file);
0 ignored issues
show
Unused Code introduced by
The call to Core\Controller\FileController::acl() has too many arguments starting with $file. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
        $this->/** @scrutinizer ignore-call */ 
108
               acl($file);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
108
109 1
        $headers=$response->getHeaders();
110
111 1
        $headers->addHeaderline('Content-Type', $file->getType())
112 1
            ->addHeaderline('Content-Length', $file->getLength());
113
114 1
        if ($file instanceof OrganizationImage) {
115 1
            $expireDate = new \DateTime();
116 1
            $expireDate->add(new \DateInterval('P1Y'));
117
118
//            $headers->addHeaderline('Expires', $expireDate->format(\DateTime::W3C))
119
//                ->addHeaderLine('ETag', $file->getId())
120
//                ->addHeaderline('Cache-Control', 'public')
121
//                ->addHeaderline('Pragma', 'cache');
122
        }
123
124 1
        $response->sendHeaders();
125
        
126 1
        $resource = $file->getResource();
127
        
128 1
        while (!feof($resource)) {
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type stream; however, parameter $handle of feof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

128
        while (!feof(/** @scrutinizer ignore-type */ $resource)) {
Loading history...
129 1
            echo fread($resource, 1024);
0 ignored issues
show
Bug introduced by
It seems like $resource can also be of type stream; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
            echo fread(/** @scrutinizer ignore-type */ $resource, 1024);
Loading history...
130
        }
131 1
        return $response;
132
    }
133
    
134 2
    public function deleteAction()
135
    {
136 2
        $file = $this->getFile();
137 2
        if (!$file) {
138 1
            $this->response->setStatusCode(500);
139 1
            $message = ($ex = $this->getEvent()->getParam('exception')) ? $ex->getMessage() : 'File not found.';
140 1
            return new JsonModel(
141
                array(
142 1
                    'result' => false,
143 1
                    'message' => $message
144
                )
145
            );
146
        }
147
        
148 1
        $this->acl($file, PermissionsInterface::PERMISSION_CHANGE);
0 ignored issues
show
Unused Code introduced by
The call to Core\Controller\FileController::acl() has too many arguments starting with $file. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
        $this->/** @scrutinizer ignore-call */ 
149
               acl($file, PermissionsInterface::PERMISSION_CHANGE);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
149
150
151
        /* @var \Core\EventManager\EventManager $events */
152 1
        $events = $this->coreFileEvents;
153 1
        $event = $events->getEvent(FileEvent::EVENT_DELETE, $this, ['file' => $file]);
154
        $results = $events->triggerEventUntil(function ($r) {
155
            return true === $r;
156 1
        }, $event);
157
158 1
        if (true !== $results->last()) {
159 1
            $this->repositories->remove($file);
160
        }
161
162 1
        return new JsonModel(
163
            array(
164 1
            'result' => true
165
            )
166
        );
167
    }
168
}
169