Test Failed
Push — 1.0.x ( 841346...2e14c5 )
by Koldo
02:55
created

ToggleProvider::enableAPI()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pheature\Community\Laravel;
6
7
use Closure;
8
use Illuminate\Support\Facades\Route;
9
use Illuminate\Support\ServiceProvider;
10
use Nyholm\Psr7\Factory\Psr17Factory;
11
use Pheature\Core\Toggle\Read\ChainToggleStrategyFactory;
12
use Pheature\Core\Toggle\Read\FeatureFinder;
13
use Pheature\Core\Toggle\Write\FeatureRepository;
14
use Pheature\Crud\Psr11\Toggle\ChainToggleStrategyFactoryFactory;
15
use Pheature\Crud\Psr11\Toggle\FeatureFinderFactory;
16
use Pheature\Crud\Psr11\Toggle\FeatureRepositoryFactory;
17
use Pheature\Crud\Psr11\Toggle\ToggleConfig;
18
use Pheature\Crud\Psr7\Toggle\DeleteFeature;
19
use Pheature\Crud\Psr7\Toggle\DeleteFeatureFactory;
20
use Pheature\Crud\Psr7\Toggle\GetFeature;
21
use Pheature\Crud\Psr7\Toggle\GetFeatureFactory;
22
use Pheature\Crud\Psr7\Toggle\GetFeatures;
23
use Pheature\Crud\Psr7\Toggle\GetFeaturesFactory;
24
use Pheature\Crud\Psr7\Toggle\PatchFeature;
25
use Pheature\Crud\Psr7\Toggle\PatchFeatureFactory;
26
use Pheature\Crud\Psr7\Toggle\PostFeature;
27
use Pheature\Crud\Psr7\Toggle\PostFeatureFactory;
28
use Pheature\Dbal\Toggle\Cli\InitSchema;
29
use Pheature\Model\Toggle\SegmentFactory;
30
use Pheature\Model\Toggle\SegmentFactoryFactory;
31
use Pheature\Model\Toggle\StrategyFactory;
32
use Pheature\Model\Toggle\StrategyFactoryFactory;
33
use Pheature\Sdk\CommandRunner;
34
use Pheature\Sdk\CommandRunnerFactory;
0 ignored issues
show
Bug introduced by
The type Pheature\Sdk\CommandRunnerFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
use Psr\Http\Message\ResponseFactoryInterface;
36
use Psr\Http\Message\ServerRequestInterface;
37
38
final class ToggleProvider extends ServiceProvider
39
{
40
    /**
41
     * Register services.
42
     *
43
     * @return void
44
     */
45 1
    public function register(): void
46
    {
47
        /** @var null|array<string, mixed>  $configItem */
48 1
        $configItem = config('pheature_flags');
49 1
        if (null === $configItem) {
50 1
            return;
51
        }
52
        $toggleConfig = new ToggleConfig($configItem);
53
        $this->app->bind(ToggleConfig::class, fn() => $toggleConfig);
54
        $this->app->bind(StrategyFactory::class, Closure::fromCallable(new StrategyFactoryFactory()));
55
        $this->app->bind(SegmentFactory::class, Closure::fromCallable(new SegmentFactoryFactory()));
56
        foreach ($toggleConfig->strategyTypes() as $strategyType) {
57
            $this->app->bind($strategyType['type'], $strategyType['factory_id']);
58
        }
59
        foreach ($toggleConfig->segmentTypes() as $segmentType) {
60
            $this->app->bind($segmentType['type'], $segmentType['factory_id']);
61
        }
62
        $this->app->bind(
63
            ChainToggleStrategyFactory::class,
64
            Closure::fromCallable(new ChainToggleStrategyFactoryFactory())
65
        );
66
        $this->app->bind(ResponseFactoryInterface::class, static fn() => new Psr17Factory());
67
        $this->app->bind(FeatureRepository::class, Closure::fromCallable(new FeatureRepositoryFactory()));
68
        $this->app->bind(FeatureFinder::class, Closure::fromCallable(new FeatureFinderFactory()));
69
        $this->app->extend(
70
            ServerRequestInterface::class,
71
            Closure::fromCallable(new RouteParameterAsPsr7RequestAttribute($this->app))
72
        );
73
74
        $this->enableSDK();
75
        $this->enableAPI($toggleConfig);
76
        $this->enableDBAL();
77
    }
78
79
    /**
80
     * Bootstrap services.
81
     *
82
     * @return void
83
     */
84 1
    public function boot(): void
85
    {
86 1
        $this->publishes(
87
            [
88 1
            __DIR__ . '/../config/pheature_flags.php' => config_path('pheature_flags.php'),
89
            ],
90 1
            'config'
91
        );
92
93 1
        Route::group(
94 1
            $this->routeConfiguration(),
95 1
            function () {
96 1
                $this->loadRoutesFrom(__DIR__ . '/../routes/pheature_flags.php');
97 1
            }
98
        );
99 1
    }
100
101
    /**
102
     * @return array<string, mixed>
103
     */
104 1
    private function routeConfiguration(): array
105
    {
106
        return [
107 1
            'prefix' => config('pheature_flags.api_prefix') ?? '',
108 1
            'middleware' => config('pheature_flags.middleware') ?? ['api'],
109
        ];
110
    }
111
112
    private function enableAPI(ToggleConfig $toggleConfig): void
113
    {
114
        if ($toggleConfig->apiEnabled()) {
115
            $this->app->bind(GetFeature::class, Closure::fromCallable(new GetFeatureFactory()));
116
            $this->app->bind(GetFeatures::class, Closure::fromCallable(new GetFeaturesFactory()));
117
            $this->app->bind(PostFeature::class, Closure::fromCallable(new PostFeatureFactory()));
118
            $this->app->bind(PatchFeature::class, Closure::fromCallable(new PatchFeatureFactory()));
119
            $this->app->bind(DeleteFeature::class, Closure::fromCallable(new DeleteFeatureFactory()));
120
        }
121
    }
122
123
    private function enableSDK(): void
124
    {
125
        if (class_exists(CommandRunner::class)) {
126
            $this->app->bind(CommandRunner::class, Closure::fromCallable(new CommandRunnerFactory()));
127
        }
128
    }
129
130
    private function enableDBAL(): void
131
    {
132
        if ('dbal' === config('pheature_flags.driver')) {
133
            $this->commands([InitSchema::class]);
134
        }
135
    }
136
}
137