Completed
Push — 3.x ( 38b337...555c81 )
by Jordi Sala
03:08
created

AdminExtractor::setPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Translator\Extractor;
15
16
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
17
use Sonata\AdminBundle\Admin\Pool;
18
use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
19
use Symfony\Component\Translation\Extractor\ExtractorInterface;
20
use Symfony\Component\Translation\MessageCatalogue;
21
22
/**
23
 * @internal
24
 */
25
final class AdminExtractor implements ExtractorInterface, LabelTranslatorStrategyInterface
26
{
27
    private const PUBLIC_ADMIN_METHODS = [
28
        'getShow',
29
        'getDatagrid',
30
        'getList',
31
        'getForm',
32
    ];
33
34
    private const BREADCRUMB_ACTIONS = [
35
        'list',
36
        'edit',
37
        'create',
38
        'update',
39
        'batch',
40
        'delete',
41
    ];
42
43
    /**
44
     * @var string
45
     */
46
    private $prefix = '';
47
48
    /**
49
     * @var MessageCatalogue|null
50
     */
51
    private $catalogue;
52
53
    /**
54
     * @var Pool
55
     */
56
    private $adminPool;
57
58
    /**
59
     * @var LabelTranslatorStrategyInterface|null
60
     */
61
    private $labelStrategy;
62
63
    /**
64
     * @var string|null
65
     */
66
    private $domain;
67
68
    /**
69
     * @var BreadcrumbsBuilderInterface
70
     */
71
    private $breadcrumbsBuilder;
72
73
    public function __construct(Pool $adminPool, BreadcrumbsBuilderInterface $breadcrumbsBuilder)
74
    {
75
        $this->adminPool = $adminPool;
76
        $this->breadcrumbsBuilder = $breadcrumbsBuilder;
77
    }
78
79
    public function extract($resource, MessageCatalogue $catalogue)
80
    {
81
        $this->catalogue = $catalogue;
82
83
        foreach ($this->adminPool->getAdminGroups() as $name => $group) {
84
            $catalogue->set($name, $this->prefix.$name, $group['label_catalogue']);
85
        }
86
87
        foreach ($this->adminPool->getAdminServiceIds() as $id) {
88
            $admin = $this->adminPool->getInstance($id);
89
90
            $this->labelStrategy = $admin->getLabelTranslatorStrategy();
91
            $this->domain = $admin->getTranslationDomain();
92
93
            $label = $admin->getLabel();
94
            if (!empty($label)) {
95
                $catalogue->set($label, $this->prefix.$label, $admin->getTranslationDomain());
96
            }
97
98
            $admin->setLabelTranslatorStrategy($this);
99
100
            foreach (self::PUBLIC_ADMIN_METHODS as $method) {
101
                $admin->$method();
102
            }
103
104
            foreach (self::BREADCRUMB_ACTIONS as $action) {
105
                $this->breadcrumbsBuilder->getBreadcrumbs($admin, $action);
106
            }
107
        }
108
    }
109
110
    public function setPrefix($prefix): void
111
    {
112
        $this->prefix = $prefix;
113
    }
114
115
    public function getLabel($label, $context = '', $type = ''): string
116
    {
117
        $label = $this->labelStrategy->getLabel($label, $context, $type);
118
119
        $this->catalogue->set($label, $this->prefix.$label, $this->domain);
120
121
        return $label;
122
    }
123
}
124