AjaxResponse::fromException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BWC\Share\Symfony\Response;
4
5
use Symfony\Component\HttpFoundation\JsonResponse;
6
7
class AjaxResponse
8
{
9
    /** @var bool */
10
    public $success = true;
11
12
    /** @var string[] */
13
    public $errors = [];
14
15
    /** @var mixed */
16
    public $data;
17
18
19
    /**
20
     * @param \Exception $e
21
     * @return AjaxResponse
22
     */
23
    public static function fromException(\Exception $e)
24
    {
25
        $response = new AjaxResponse();
26
        $response->success = false;
27
        $response->errors[] = $e->getMessage();
28
29
        return $response;
30
    }
31
32
33
    /**
34
     * @param $data
35
     * @return AjaxResponse
36
     */
37
    public static function fromJsonEncodable($data)
38
    {
39
        $response = new AjaxResponse();
40
        $response->data = $data;
41
42
        return $response;
43
    }
44
45
46
    /**
47
     * @param int $status
48
     * @return JsonResponse
49
     */
50
    public function toJsonResponse($status = 200)
51
    {
52
        return new JsonResponse($this, $status);
53
    }
54
}