SitemapProvider::getExtensionNameByClassName()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 4
nc 6
nop 1
1
<?php
2
3
/**
4
 * Sitemap provider
5
 *
6
 * @author Tim Lochmüller
7
 */
8
9
namespace FRUIT\GoogleServices\Service;
10
11
use TYPO3\CMS\Core\Utility\GeneralUtility;
12
use TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException;
13
use TYPO3\CMS\Extbase\Object\ObjectManager;
14
15
/**
16
 * Description of SitemapProvider
17
 */
18
class SitemapProvider
19
{
20
21
    /**
22
     * Provider Storage
23
     *
24
     * @var array
25
     */
26
    private static $provider = [];
27
28
    /**
29
     * Add a Sitemap Provider
30
     *
31
     * @param string $className
32
     */
33
    public static function addProvider($className)
34
    {
35
        self::$provider[$className] = $className;
36
    }
37
38
    /**
39
     * Get all Providers
40
     *
41
     * @return array
42
     */
43
    public static function getProviders()
44
    {
45
        return self::$provider;
46
    }
47
48
    /**
49
     * Get a provider
50
     *
51
     * @param string $name
52
     *
53
     * @throws InvalidArgumentValueException
54
     * @return SitemapProviderInterface
55
     */
56
    public static function getProvider($name)
57
    {
58
        if (!isset(self::$provider[$name])) {
59
            throw new InvalidArgumentValueException($name . ' not exists');
60
        }
61
        $obj = new ObjectManager();
62
        $return = $obj->get($name);
63
        if (!($return instanceof SitemapProviderInterface)) {
64
            throw new InvalidArgumentValueException($name . ' has to implement the SitemapProviderInterface interface');
65
        }
66
        return $return;
67
    }
68
69
    /**
70
     * Flex form selection
71
     *
72
     * @param array $params
73
     * @param object $ref
74
     */
75
    public function flexformSelection(&$params, &$ref)
0 ignored issues
show
Unused Code introduced by
The parameter $ref is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
    {
77
        $providers = self::getProviders();
78
        $params['items'] = [];
79
80
81
        foreach ($providers as $provider) {
82
            $extensionName = self::getExtensionNameByClassName($provider);
83
            $params['items'][] = [
84
                $provider,
85
                $provider,
86
                'EXT:' . GeneralUtility::camelCaseToLowerCaseUnderscored($extensionName) . '/ext_icon.gif',
87
            ];
88
        }
89
    }
90
91
    /**
92
     * Get extension name by class name
93
     *
94
     * @param $className
95
     *
96
     * @return array
97
     */
98
    public static function getExtensionNameByClassName($className)
99
    {
100
        $matches = [];
101
        if (strpos($className, '\\') !== false) {
102
            if (substr($className, 0, 9) === 'TYPO3\\CMS') {
103
                $extensionName = '^(?P<vendorName>[^\\\\]+\\\[^\\\\]+)\\\(?P<extensionName>[^\\\\]+)';
104
            } else {
105
                $extensionName = '^(?P<vendorName>[^\\\\]+)\\\\(?P<extensionName>[^\\\\]+)';
106
            }
107
            preg_match('/' . $extensionName . '\\\\.*$/ix', $className, $matches);
108
        } else {
109
            preg_match('/^Tx_(?P<extensionName>[^_]+)_.*$/ix', $className, $matches);
110
        }
111
        return isset($matches['extensionName']) ? $matches['extensionName'] : null;
112
    }
113
}
114