Passed
Push — master ( ad6b2a...b41ca0 )
by Caen
07:45 queued 14s
created

HasKernelData   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseConfigurationAuthors() 0 12 2
A authors() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Concerns;
6
7
use Hyde\Facades\Config;
8
use Illuminate\Support\Collection;
9
use Hyde\Framework\Features\Blogging\Models\PostAuthor;
0 ignored issues
show
Bug introduced by
The type Hyde\Framework\Features\Blogging\Models\PostAuthor 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...
10
use Hyde\Framework\Exceptions\InvalidConfigurationException;
11
12
use function collect;
13
14
/**
15
 * Contains accessors and containers for general data stored in the kernel.
16
 *
17
 * @internal Single-use trait for the HydeKernel class.
18
 *
19
 * @see \Hyde\Foundation\HydeKernel
20
 */
21
trait HasKernelData
22
{
23
    /**
24
     * The collection of authors defined in the config.
25
     *
26
     * @var \Illuminate\Support\Collection<string, \Hyde\Framework\Features\Blogging\Models\PostAuthor>
27
     */
28
    protected Collection $authors;
29
30
    /**
31
     * Get the collection of authors defined in the config.
32
     *
33
     * @return \Illuminate\Support\Collection<string, \Hyde\Framework\Features\Blogging\Models\PostAuthor>
34
     */
35
    public function authors(): Collection
36
    {
37
        if (isset($this->authors)) {
38
            return $this->authors;
39
        }
40
41
        $config = collect(Config::getArray('hyde.authors', []));
0 ignored issues
show
Bug introduced by
Hyde\Facades\Config::get...hyde.authors', array()) of type array 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

41
        $config = collect(/** @scrutinizer ignore-type */ Config::getArray('hyde.authors', []));
Loading history...
42
43
        if ($config->isEmpty()) {
44
            // Defer setting the authors property until the next try.
45
            return $config;
46
        }
47
48
        return $this->authors = $this->parseConfigurationAuthors($config);
49
    }
50
51
    protected function parseConfigurationAuthors(Collection $authors): Collection
52
    {
53
        return $authors->mapWithKeys(function (array $author, string $username): array {
54
            if (! $username) {
55
                throw new InvalidConfigurationException('Author username cannot be empty. Did you forget to set the author\'s array key?', 'hyde', 'authors');
56
            }
57
58
            $username = PostAuthor::normalizeUsername($username);
59
60
            $author['username'] = $username;
61
62
            return [$username => PostAuthor::create($author)];
63
        });
64
    }
65
}
66