Passed
Push — main ( cf886d...784101 )
by Michael
03:16
created

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\Str;
8
use MichaelRubel\Formatters\Exceptions\ShouldImplementInterfaceException;
9
use MichaelRubel\Formatters\Exceptions\ShouldNotUseCamelCaseException;
10
use Spatie\LaravelPackageTools\Package;
11
use Spatie\LaravelPackageTools\PackageServiceProvider;
12
13
class FormatterServiceProvider extends PackageServiceProvider
14
{
15
    /**
16
     * Internal constants.
17
     *
18
     * @const
19
     */
20
    public const PACKAGE_FOLDER  = 'Collection';
21
    public const PACKAGE_CLASS   = 'Formatter';
22
    public const BINDING_POSTFIX = '_formatter';
23
    public const CLASS_SEPARATOR = '\\';
24
25
    /**
26
     * Configure the package.
27
     *
28
     * @param Package $package
29
     *
30
     * @return void
31
     */
32 15
    public function configurePackage(Package $package): void
33
    {
34
        $package
35 15
            ->name('laravel-formatters')
36 15
            ->hasConfigFile();
37 15
    }
38
39
    /**
40
     * Register any package services.
41
     *
42
     * @return void
43
     */
44 15
    public function packageRegistered(): void
45
    {
46 15
        $app_folder = config('formatters.folder')
47 1
            ?? 'app' . DIRECTORY_SEPARATOR . 'Formatters';
48
49 15
        $bindings_case = config('formatters.bindings_case')
50 1
            ?? 'snake';
51
52 15
        $filesystem = app('files');
53
54 15
        $appFormatters = $filesystem->isDirectory($app_folder)
55 15
            ? collect($filesystem->allFiles($app_folder))
56 1
            : collect();
57
58 15
        $packageFormatters = collect(
59 15
            $filesystem->allFiles(
60 15
                $this->getPackageBaseDir()
61 15
                . DIRECTORY_SEPARATOR
62 15
                . self::PACKAGE_FOLDER
63
            )
64
        );
65
66
        $packageFormatters
67 15
            ->merge($appFormatters)
68 15
            ->each(function ($file) use ($app_folder, $bindings_case) {
69 15
                $filename = $file->getFilenameWithoutExtension();
70 15
                $name     = $this->getFormatterName($bindings_case, $filename);
71 15
                $class    = $this->getFormatterClass($file, $filename, $app_folder);
72
73 15
                $this->app->bind($name, $class);
74 15
            });
75 15
    }
76
77
    /**
78
     * Returns formatter name for string binding.
79
     *
80
     * @param string $bindings_case
81
     * @param string $filename
82
     *
83
     * @return string
84
     */
85 15
    public function getFormatterName(string $bindings_case, string $filename): string
86
    {
87 15
        $name = str_replace('Formatter', '', $filename);
88
89 15
        return Str::{$bindings_case}($name . self::BINDING_POSTFIX);
90
    }
91
92
    /**
93
     * Determines the formatter class namespace.
94
     *
95
     * @param object $file
96
     * @param string $filename
97
     * @param string $app_folder
98
     *
99
     * @return string
100
     */
101 15
    private function getFormatterClass(object $file, string $filename, string $app_folder): string
102
    {
103 15
        $path = str_contains($file->getPathName(), $app_folder)
104 15
            ? Str::ucfirst(str_replace(DIRECTORY_SEPARATOR, self::CLASS_SEPARATOR, $app_folder))
105 15
              . self::CLASS_SEPARATOR
106 15
            : (new \ReflectionClass(static::class))->getNamespaceName()
107 15
              . self::CLASS_SEPARATOR
108 15
              . self::PACKAGE_FOLDER
109 15
              . self::CLASS_SEPARATOR;
110
111 15
        return sprintf('%s%s', $path, $filename);
112
    }
113
114
    /**
115
     * Ensures all the formatters will implement the same interface.
116
     *
117
     * @param object $formatter
118
     */
119 14
    public static function ensureFormatterImplementsInterface(object $formatter): void
120
    {
121 14
        if (! $formatter instanceof Formatter) {
122 2
            if (config('formatters.bindings_case') === 'camel') {
123 1
                throw new ShouldNotUseCamelCaseException();
124
            }
125
126 1
            throw new ShouldImplementInterfaceException();
127
        }
128 12
    }
129
}
130