Issues (113)

src/Exception/ActionChargingException.php (2 issues)

1
<?php
2
declare(strict_types=1);
3
4
namespace hiqdev\php\billing\Exception;
5
6
use hiqdev\php\billing\action\ActionInterface;
7
use Throwable;
8
9
/**
10
 * Class ActionChargingException should be thrown when producing charges
11
 * for the Action have lead to an exception.
12
 *
13
 * @author Dmytro Naumenko <[email protected]>
14
 */
15
final class ActionChargingException extends RuntimeException
16
{
17
    private readonly ActionInterface $action;
18
19
    public static function forAction(ActionInterface $action, Throwable $previousException): self
20
    {
21
        if ($action->getId() !== null) {
0 ignored issues
show
The condition $action->getId() !== null is always true.
Loading history...
22
            $message = sprintf('Failed to charge action %s: %s', $action->getId(), $previousException->getMessage());
23
        } else {
24
            $message = sprintf(
25
                'Failed to charge action (type: %s, target: %s, quantity: %s %s, customer: %s, time: %s): %s',
26
                $action->getType()->getName(), $action->getTarget()->getUniqueId(),
27
                $action->getQuantity()->getQuantity(), $action->getQuantity()->getUnit()->getName(),
28
                $action->getCustomer()->getLogin(),
29
                $action->getTime()->format(DATE_ATOM),
30
                $previousException->getMessage()
31
            );
32
        }
33
34
        $self = new self($message, 0, $previousException);
35
        $self->action = $action;
0 ignored issues
show
The property action is declared read-only in hiqdev\php\billing\Excep...ActionChargingException.
Loading history...
36
37
        return $self;
38
    }
39
40
    public function getAction(): ActionInterface
41
    {
42
        return $this->action;
43
    }
44
}
45