Completed
Pull Request — master (#151)
by
unknown
01:52
created

ImageUploadAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 4
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 8 1
1
<?php
2
3
namespace Admin\Action;
4
5
use Psr\Http\Message\ResponseInterface as Response;
6
use Psr\Http\Message\ServerRequestInterface as RequestInterface;
7
use Zend\Http\PhpEnvironment\Request;
8
use Zend\Diactoros\Response\JsonResponse;
9
use UploadHelper\Upload;
10
11
/**
12
 * Class ImageUploadAction.
13
 */
14
final class ImageUploadAction
15
{
16
17
    /**
18
     * IndexAction constructor.
19
     *
20
     * @param Upload $upload template engine
21
     */
22
    public function __construct(Upload $upload)
23
    {
24
        $this->upload = $upload;
0 ignored issues
show
Bug introduced by
The property upload does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
    }
26
27
    /**
28
     * Executed when action is called.
29
     *
30
     * @param RequestInterface       $request  request
31
     * @param Response      $response response
32
     * @param callable|null $next     next middleware
33
     *
34
     * @return JsonResponse
35
     */
36
    public function __invoke(RequestInterface $request, Response $response, callable $next = null)
37
    {
38
        $data = $request->getParsedBody();
39
        $data += (new Request())->getFiles()->toArray();
40
        $imagePath = $this->upload->uploadImage($data, 'file');
41
42
        return new JsonResponse(['location' => $imagePath]);
43
    }
44
}
45