Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
SourceSiteLocator
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSourceSite
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace FileImporter\Services;
4
5use FileImporter\Data\SourceUrl;
6use FileImporter\Exceptions\SourceUrlException;
7
8/**
9 * SourceSiteLocator for getting a SourceSite service which can handle a given URL.
10 *
11 * @license GPL-2.0-or-later
12 * @author Addshore
13 */
14class SourceSiteLocator {
15
16    /** @var SourceSite[] */
17    private array $sourceSites;
18
19    /**
20     * @param SourceSite[] $sourceSites
21     */
22    public function __construct( array $sourceSites ) {
23        $this->sourceSites = $sourceSites;
24    }
25
26    /**
27     * @throws SourceUrlException when the URL doesn't match a known site
28     */
29    public function getSourceSite( SourceUrl $sourceUrl ): SourceSite {
30        foreach ( $this->sourceSites as $site ) {
31            if ( $site->isSourceSiteFor( $sourceUrl ) ) {
32                return $site;
33            }
34        }
35
36        throw new SourceUrlException();
37    }
38
39}