Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ActionResponseStatus
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
5.27
0.00% covered (danger)
0.00%
0 / 1
 createFromError
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
5.27
1<?php
2namespace Apie\Core\Actions;
3
4use Apie\Core\Exceptions\ActionNotAllowedException;
5use Apie\Core\Exceptions\EntityNotFoundException;
6use Doctrine\ORM\Exception\ORMException;
7use Exception;
8
9enum ActionResponseStatus: string
10{
11    /**
12     * Resource was properly created.
13     */
14    case CREATED = 'created';
15    /**
16     * Action was properly executed.
17     */
18    case SUCCESS = 'success';
19
20    /**
21     * Resource was properly deleted
22     */
23    case DELETED = 'deleted';
24
25    /**
26     * Permission denied or authorization required error
27     */
28    case AUTHORIZATION_ERROR = 'authorization_error';
29
30    /**
31     * There is something wrong with the input.
32     * For example if it is an API CALL the request body has invalid data.
33     */
34    case CLIENT_ERROR = 'client_error';
35
36    /**
37     * {id} placeholder in route could not be found.
38     */
39    case NOT_FOUND = 'not_found';
40
41    /**
42     * There is something wrong with storing the entity.
43     *
44     * For example: database error, unique constraints, etc.
45     */
46    case PERISTENCE_ERROR = 'persistence_error';
47
48    /**
49     * There is something wrong with displaying the result of the action.
50     */
51    case OUTPUT_ERROR = 'output_error';
52
53    /**
54     * Any other error is considered a server error.
55     */
56    case SERVER_ERROR = 'server_error';
57
58    public static function createFromError(\Throwable $error): self
59    {
60        if ($error instanceof ActionNotAllowedException) {
61            return self::AUTHORIZATION_ERROR;
62        }
63        if (!($error instanceof Exception)) {
64            return self::SERVER_ERROR;
65        }
66        if ($error instanceof EntityNotFoundException) {
67            return self::NOT_FOUND;
68        }
69        if ($error instanceof ORMException) {
70            return self::PERISTENCE_ERROR;
71        }
72        return self::CLIENT_ERROR;
73    }
74}