Completed
Push — 1.4 ( f80e1e...b01c64 )
by Paweł
17s queued 10s
created

ArticleResolver::getFragmentFromUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Core Bundle.
5
 *
6
 * Copyright 2018 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2018 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\CoreBundle\Resolver;
16
17
use SWP\Bundle\CoreBundle\Model\ArticleInterface;
18
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
19
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
20
21
class ArticleResolver implements ArticleResolverInterface
22
{
23
    /**
24
     * @var UrlMatcherInterface
25
     */
26
    private $matcher;
27
28
    public function __construct(UrlMatcherInterface $matcher)
29
    {
30
        $this->matcher = $matcher;
31
    }
32
33
    public function resolve(string $url): ?ArticleInterface
34
    {
35
        try {
36
            $route = $this->matcher->match($this->getFragmentFromUrl($url, 'path'));
37
            if (isset($route['_article_meta']) && $route['_article_meta']->getValues() instanceof ArticleInterface) {
38
                return $route['_article_meta']->getValues();
39
            }
40
        } catch (ResourceNotFoundException $e) {
41
            return null;
42
        }
43
    }
44
45
    private function getFragmentFromUrl(string $url, string $fragment): ?string
46
    {
47
        $fragments = \parse_url($url);
48
        if (!\array_key_exists($fragment, $fragments)) {
49
            return null;
50
        }
51
52
        return str_replace('/app_dev.php', '', $fragments[$fragment]);
53
    }
54
}
55