CircuitBreakerAwareTrait::reportServiceSuccess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace HolidayPirates\CircuitBreaker\Awareness;
4
5
use HolidayPirates\CircuitBreaker\CircuitBreakerInterface;
6
use HolidayPirates\CircuitBreaker\Exception\UnavailableServiceException;
7
8
trait CircuitBreakerAwareTrait
9
{
10
    private $circuitBreaker;
11
12
    public function setCircuitBreaker(CircuitBreakerInterface $circuitBreaker): void
13
    {
14
        $this->circuitBreaker = $circuitBreaker;
15
    }
16
17
    public function getCircuitBreaker(): CircuitBreakerInterface
18
    {
19
        return $this->circuitBreaker;
20
    }
21
22
    /**
23
     * @throws UnavailableServiceException
24
     */
25
    public function throwExceptionIfServiceUnavailable(string $serviceName): void
26
    {
27
        if (false === $this->getCircuitBreaker()->isServiceAvailable($serviceName)) {
28
            throw new UnavailableServiceException("Service {$serviceName} is not available right now.");
29
        }
30
    }
31
32
    public function reportServiceSuccess(string $serviceName): void
33
    {
34
        $this->getCircuitBreaker()->reportSuccess($serviceName);
35
    }
36
37
    public function reportServiceFailure(string $serviceName): void
38
    {
39
        $this->getCircuitBreaker()->reportFailure($serviceName);
40
    }
41
}
42