Passed
Push — main ( 517a85...da520e )
by Michael
02:27
created

FormatterServiceProvider::getFormatterClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 18
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\Formatters;
6
7
use Illuminate\Support\Facades\File;
8
use Illuminate\Support\Str;
9
use Spatie\LaravelPackageTools\Package;
10
use Spatie\LaravelPackageTools\PackageServiceProvider;
11
12
class FormatterServiceProvider extends PackageServiceProvider
13
{
14
    /**
15
     * Internal constants.
16
     *
17
     * @const
18
     */
19
    public const PACKAGE_FOLDER    = 'Collection';
20
    public const PACKAGE_NAMESPACE = '\MichaelRubel\Formatters\Collection\\';
21
    public const FORMATTER_POSTFIX = '_formatter';
22
    public const CLASS_SEPARATOR   = '\\';
23
24
    /**
25
     * Configure the package.
26
     *
27
     * @param Package $package
28
     *
29
     * @return void
30
     */
31
    public function configurePackage(Package $package): void
32
    {
33
        $package
34
            ->name('laravel-formatters')
35
            ->hasConfigFile();
36
    }
37
38
    /**
39
     * Register any package services.
40
     *
41
     * @return void
42
     */
43
    public function packageBooted(): void
44
    {
45
        $app_folder = config('formatters.folder');
46
47
        $appFormatters = File::isDirectory($app_folder)
48
            ? collect(File::allFiles($app_folder)) // @codeCoverageIgnore
49
            : collect();
50
51
        $packageFormatters = collect(
52
            File::allFiles(
53
                $this->getPackageBaseDir()
54
                . DIRECTORY_SEPARATOR
55
                . self::PACKAGE_FOLDER
56
            )
57
        );
58
59
        $appFormatters
60
            ->union($packageFormatters)
61
            ->each(function ($file) use ($app_folder) {
62
                $filename = $file->getFilenameWithoutExtension();
63
64
                $class = $this->getFormatterClass(
65
                    $file,
66
                    $filename,
67
                    $app_folder
68
                );
69
70
                $this->app->bind(
71
                    Str::snake($filename),
72
                    $class
73
                );
74
            });
75
    }
76
77
    /**
78
     * Determines the formatter class namespace.
79
     *
80
     * @param object $file
81
     * @param string $filename
82
     * @param string $app_folder
83
     *
84
     * @return string
85
     */
86
    private function getFormatterClass(object $file, string $filename, string $app_folder): string
87
    {
88
        // @codeCoverageIgnoreStart
89
        $path = str_contains($file->getPathName(), $app_folder)
90
            ? Str::ucfirst(
91
                str_replace(
92
                    '/',
93
                    self::CLASS_SEPARATOR,
94
                    $app_folder
95
                )
96
            ) . self::CLASS_SEPARATOR
97
            : self::PACKAGE_NAMESPACE;
98
        // @codeCoverageIgnoreEnd
99
100
        return sprintf(
101
            '%s%s',
102
            $path,
103
            $filename
104
        );
105
    }
106
}
107