Passed
Push — master ( 9aef17...9e5266 )
by Gabriel
05:26
created

UrlGeneratorFactory::createForMedia()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 13
ccs 5
cts 7
cp 0.7143
crap 2.0932
rs 10
c 2
b 1
f 1
1
<?php
2
3
namespace ByTIC\MediaLibrary\UrlGenerator;
4
5
use ByTIC\MediaLibrary\Media\Media;
6
use ByTIC\MediaLibrary\Media\Traits\UrlMethodsTrait;
7
use ByTIC\MediaLibrary\PathGenerator\PathGeneratorFactory;
8
9
/**
10
 * Class UrlGeneratorFactory.
11
 */
12
class UrlGeneratorFactory
13
{
14
    /**
15
     * @return BaseUrlGenerator
16
     */
17 1
    public static function create()
18
    {
19 1
        $pathGenerator = PathGeneratorFactory::create();
20
21 1
        $urlGenerator = new BaseUrlGenerator();
22 1
        $urlGenerator->setPathGenerator($pathGenerator);
23 1
        return $urlGenerator;
24
    }
25
26
    /**
27
     * @param Media|UrlMethodsTrait $media
28
     *
29
     * @param string $conversionName
30
     * @return UrlGeneratorInterface|BaseUrlGenerator
31
     * @noinspection PhpDocMissingThrowsInspection
32
     */
33 1
    public static function createForMedia(Media $media, string $conversionName = ''): UrlGeneratorInterface
34
    {
35 1
        $urlGenerator = static::create()
36 1
            ->setMedia($media);
37
38 1
        if (!empty($conversionName)) {
39
            /** @noinspection PhpUnhandledExceptionInspection */
40
            $conversion = $media->getConversions()->getByName($conversionName);
41
42
            $urlGenerator->setConversion($conversion);
43
        }
44
45 1
        return $urlGenerator;
46
    }
47
}
48