Test Failed
Push — v4 ( 12fad0...ca6617 )
by Andrew
42:57 queued 19:35
created

AssetHelper::getAssetInputSources()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 24
rs 9.4555
cc 5
nc 3
nop 0
1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) nystudio107
10
 */
11
12
namespace nystudio107\seomatic\helpers;
13
14
use Craft;
15
use craft\elements\Asset;
16
use craft\fs\Temp;
17
use craft\helpers\ArrayHelper;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, nystudio107\seomatic\helpers\ArrayHelper. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
use craft\services\ElementSources;
19
20
/**
21
 * @author    nystudio107
22
 * @package   Seomatic
23
 * @since     4.1.18
24
 */
25
class AssetHelper
26
{
27
    /**
28
     * Return asset volume sources that can be accessed by the current user
29
     *
30
     * @return array
31
     */
32
    public static function getAssetInputSources(): array
33
    {
34
        $sources = [];
35
        foreach (Craft::$app->getElementSources()->getSources(Asset::class) as $source) {
36
            if ($source['type'] !== ElementSources::TYPE_HEADING) {
37
                $sources[] = $source['key'];
38
            }
39
        }
40
41
        $userService = Craft::$app->getUser();
42
        $volumesService = Craft::$app->getVolumes();
43
        return ArrayHelper::where($sources, function(string $source) use ($volumesService, $userService) {
44
            // If it’s not a volume folder, let it through
45
            if (!str_starts_with($source, 'volume:')) {
46
                return true;
47
            }
48
            // Only show it if they have permission to view it, or if it's the temp volume
49
            $volumeUid = explode(':', $source)[1];
50
            if ($userService->checkPermission("viewAssets:$volumeUid")) {
51
                return true;
52
            }
53
            $volume = $volumesService->getVolumeByUid($volumeUid);
54
            return $volume?->getFs() instanceof Temp;
55
        }, true, true, false);
56
    }
57
}
58