Passed
Push — main ( 834936...e5772d )
by Michael
11:48
created

FormatterServiceProvider::ensureFormatterImplementsInterface()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\Formatters;
6
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Str;
9
use MichaelRubel\EnhancedContainer\LecServiceProvider;
10
use MichaelRubel\Formatters\Commands\MakeFormatterCommand;
11
use Spatie\LaravelPackageTools\Package;
12
use Spatie\LaravelPackageTools\PackageServiceProvider;
13
use Symfony\Component\Finder\SplFileInfo;
14
15
class FormatterServiceProvider extends PackageServiceProvider
16
{
17
    /**
18
     * Internal constants.
19
     *
20
     * @const
21
     */
22
    public const PACKAGE_FOLDER  = 'Collection';
23
    public const BINDING_POSTFIX = '_formatter';
24
    public const CLASS_SEPARATOR = '\\';
25
26
    /**
27
     * Configure the package.
28
     *
29
     * @param Package $package
30
     *
31
     * @return void
32
     */
33 55
    public function configurePackage(Package $package): void
34
    {
35
        $package
36 55
            ->name('laravel-formatters')
37 55
            ->hasConfigFile()
38 55
            ->hasCommand(MakeFormatterCommand::class);
39
    }
40
41
    /**
42
     * Register any package services.
43
     *
44
     * @return void
45
     */
46 55
    public function packageRegistered(): void
47
    {
48 55
        $this->app->register(LecServiceProvider::class);
49
50
        /** @var string */
51 55
        $app_folder = config('formatters.folder')
52 1
            ?? 'app' . DIRECTORY_SEPARATOR . 'Formatters';
53
54
        /** @var string */
55 55
        $bindings_case = config('formatters.bindings_case')
56
            ?? 'kebab';
57
58 55
        $filesystem = app('files');
59
60 55
        $appFormatters = $filesystem->isDirectory(base_path($app_folder))
61 54
            ? collect($filesystem->allFiles(base_path($app_folder)))
0 ignored issues
show
Bug introduced by
$filesystem->allFiles(base_path($app_folder)) of type Symfony\Component\Finder\SplFileInfo[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
            ? collect(/** @scrutinizer ignore-type */ $filesystem->allFiles(base_path($app_folder)))
Loading history...
62 2
            : new Collection;
63
64 55
        $packageFormatters = collect(
65 55
            $filesystem->allFiles(
66 55
                $this->getPackageBaseDir()
67
                . DIRECTORY_SEPARATOR
68
                . self::PACKAGE_FOLDER
69
            )
70
        );
71
72
        $packageFormatters
73 55
            ->merge($appFormatters)
74 55
            ->each(function ($file) use ($app_folder, $bindings_case) {
75 55
                $filename = $file->getFilenameWithoutExtension();
76 55
                $name     = $this->getFormatterName($bindings_case, $filename);
77 55
                $class    = $this->getFormatterClass($file, $filename, $app_folder);
78
79 55
                $this->app->bind($name, $class);
80
            });
81
    }
82
83
    /**
84
     * Returns formatter name for string binding.
85
     *
86
     * @param string $bindings_case
87
     * @param string $filename
88
     *
89
     * @return string
90
     */
91 55
    public function getFormatterName(string $bindings_case, string $filename): string
92
    {
93 55
        $name = str_replace('Formatter', '', $filename);
94
95 55
        return Str::{$bindings_case}($name . self::BINDING_POSTFIX);
96
    }
97
98
    /**
99
     * Determines the formatter class namespace.
100
     *
101
     * @param SplFileInfo $file
102
     * @param string      $filename
103
     * @param string      $app_folder
104
     *
105
     * @return string
106
     */
107 55
    private function getFormatterClass(SplFileInfo $file, string $filename, string $app_folder): string
108
    {
109 55
        $path = str_contains($file->getPathName(), $app_folder)
110 54
            ? Str::ucfirst(str_replace(DIRECTORY_SEPARATOR, self::CLASS_SEPARATOR, $app_folder))
111
              . self::CLASS_SEPARATOR
112 55
            : (new \ReflectionClass(static::class))->getNamespaceName()
113
              . self::CLASS_SEPARATOR
114
              . self::PACKAGE_FOLDER
115
              . self::CLASS_SEPARATOR;
116
117 55
        return sprintf('%s%s', $path, $filename);
118
    }
119
}
120