Completed
Push — master ( 74b41d...2c613a )
by Tobias
02:18
created

ResponseFactory::createInternalError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Happyr\ApiBundle\Service;
4
5
use League\Fractal\Resource\Collection;
6
use League\Fractal\Resource\Item;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use League\Fractal\Manager;
9
10
/**
11
 * @author Tobias Nyholm <[email protected]>
12
 */
13
final class ResponseFactory
14
{
15
    const CODE_WRONG_ARGS = 'GEN-ARGUMENTS';
16
    const CODE_NOT_FOUND = 'GEN-NOTFOUND';
17
    const CODE_INTERNAL_ERROR = 'GEN-SERVERERROR';
18
    const CODE_UNAUTHORIZED = 'GEN-UNAUTHORIZED';
19
    const CODE_FORBIDDEN = 'GEN-FORBIDDEN';
20
21
    /**
22
     * @var Manager
23
     */
24
    private $fractal;
25
26
    /**
27
     * @param Manager $fractal
28
     */
29
    public function __construct(Manager $fractal)
30
    {
31
        $this->fractal = $fractal;
32
    }
33
34
    /**
35
     * @param mixed $item
36
     * @param $callback
37
     *
38
     * @return JsonResponse
39
     */
40 View Code Duplication
    public function createWithItem($item, $callback)
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...
41
    {
42
        $resource = new Item($item, $callback);
43
        $rootScope = $this->fractal->createData($resource);
44
45
        return $this->createWithArray($rootScope->toArray());
0 ignored issues
show
Bug introduced by
It seems like $rootScope->toArray() targeting League\Fractal\Scope::toArray() can also be of type null; however, Happyr\ApiBundle\Service...tory::createWithArray() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
46
    }
47
48
    /**
49
     * @param mixed $collection
50
     * @param $callback
51
     *
52
     * @return JsonResponse
53
     */
54 View Code Duplication
    public function createWithCollection($collection, $callback)
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...
55
    {
56
        $resource = new Collection($collection, $callback);
57
        $rootScope = $this->fractal->createData($resource);
58
59
        return $this->createWithArray($rootScope->toArray());
0 ignored issues
show
Bug introduced by
It seems like $rootScope->toArray() targeting League\Fractal\Scope::toArray() can also be of type null; however, Happyr\ApiBundle\Service...tory::createWithArray() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
60
    }
61
62
    /**
63
     * @param array $array
64
     * @param int   $statusCode
65
     * @param array $headers
66
     *
67
     * @return JsonResponse
68
     */
69
    public function createWithArray(array $array, $statusCode = 200, array $headers = [])
70
    {
71
        return new JsonResponse($array, $statusCode, $headers);
72
    }
73
74
    /**
75
     * @param string $message
76
     * @param int    $statusCode
77
     * @param int    $errorCode
78
     *
79
     * @return JsonResponse
80
     */
81
    public function createWithError($message, $statusCode, $errorCode)
82
    {
83
        if ($statusCode === 200) {
84
            trigger_error(
85
                'You better have a really good reason for erroring on a 200...',
86
                E_USER_WARNING
87
            );
88
        }
89
90
        return $this->createWithArray([
91
            'error' => [
92
                'code' => $errorCode,
93
                'http_code' => $statusCode,
94
                'message' => $message,
95
            ],
96
        ], $statusCode);
97
    }
98
99
    /**
100
     * Generates a Response with a 403 HTTP header and a given message.
101
     *
102
     * @return JsonResponse
103
     */
104
    public function createForbidden($message = 'Forbidden')
105
    {
106
        return $this->createWithError($message, 403, self::CODE_FORBIDDEN);
107
    }
108
109
    /**
110
     * Generates a Response with a 500 HTTP header and a given message.
111
     *
112
     * @return JsonResponse
113
     */
114
    public function createInternalError($message = 'Internal Error')
115
    {
116
        return $this->createWithError($message, 500, self::CODE_INTERNAL_ERROR);
117
    }
118
119
    /**
120
     * Generates a Response with a 404 HTTP header and a given message.
121
     *
122
     * @return JsonResponse
123
     */
124
    public function createNotFound($message = 'Resource Not Found')
125
    {
126
        return $this->createWithError($message, 404, self::CODE_NOT_FOUND);
127
    }
128
129
    /**
130
     * Generates a Response with a 401 HTTP header and a given message.
131
     *
132
     * @return JsonResponse
133
     */
134
    public function createUnauthorized($message = 'Unauthorized')
135
    {
136
        return $this->createWithError($message, 401, self::CODE_UNAUTHORIZED);
137
    }
138
139
    /**
140
     * Generates a Response with a 400 HTTP header and a given message.
141
     *
142
     * @return JsonResponse
143
     */
144
    public function createWrongArgs($message = 'Wrong Arguments')
145
    {
146
        return $this->createWithError($message,  400, self::CODE_WRONG_ARGS);
147
    }
148
}
149