• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

plank / laravel-mediable / 3830978892

pending completion
3830978892

push

github

GitHub
Merge pull request #300 from plank/scoped-disk-support

3 of 4 new or added lines in 1 file covered. (75.0%)

1204 of 1262 relevant lines covered (95.4%)

118.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.33
/src/UrlGenerators/UrlGeneratorFactory.php
1
<?php
2
declare(strict_types=1);
3

4
namespace Plank\Mediable\UrlGenerators;
5

6
use Plank\Mediable\Exceptions\MediaUrlException;
7
use Plank\Mediable\Media;
8

9
class UrlGeneratorFactory
10
{
11
    /**
12
     * map of UrlGenerator classes to use for different filesystem drivers.
13
     * @var string[]
14
     */
15
    protected $driver_generators = [];
16

17
    /**
18
     * Get a UrlGenerator instance for a media.
19
     * @param  Media $media
20
     * @return UrlGeneratorInterface
21
     * @throws MediaUrlException If no generator class has been assigned for the media's disk's driver
22
     */
23
    public function create(Media $media): UrlGeneratorInterface
24
    {
25
        $driver = $this->getDriverForDisk($media->disk);
84✔
26
        if (array_key_exists($driver, $this->driver_generators)) {
84✔
27
            $class = $this->driver_generators[$driver];
78✔
28

29
            $generator = app($class);
78✔
30
            $generator->setMedia($media);
78✔
31

32
            return $generator;
78✔
33
        }
34

35
        throw MediaUrlException::generatorNotFound($media->disk, $driver);
6✔
36
    }
37

38
    /**
39
     * Set a generator subclass to use for media on a disk with a particular driver.
40
     * @param string $class
41
     * @param string $driver
42
     * @return void
43
     *
44
     * @throws MediaUrlException
45
     */
46
    public function setGeneratorForFilesystemDriver(string $class, string $driver): void
47
    {
48
        $this->validateGeneratorClass($class);
84✔
49
        $this->driver_generators[$driver] = $class;
78✔
50
    }
39✔
51

52
    /**
53
     * Verify that a class name is a valid generator.
54
     * @param  string $class
55
     * @return void
56
     *
57
     * @throws MediaUrlException If class does not exist or does not implement `UrlGenerator`
58
     */
59
    protected function validateGeneratorClass(string $class): void
60
    {
61
        if (!class_exists($class) || !is_subclass_of($class, UrlGeneratorInterface::class)) {
84✔
62
            throw MediaUrlException::invalidGenerator($class);
6✔
63
        }
64
    }
39✔
65

66
    /**
67
     * Get the driver used by a specified disk.
68
     * @param  string $disk
69
     * @return string
70
     */
71
    protected function getDriverForDisk(string $disk): string
72
    {
73
        $driver = (string) config("filesystems.disks.{$disk}.driver");
84✔
74
        if ($driver === 'scoped') {
84✔
NEW
75
            return $this->getDriverForDisk(config("filesystems.disks.{$disk}.disk"));
×
76
        }
77
        return $driver;
84✔
78
    }
79
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc