Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
29.63% covered (danger)
29.63%
8 / 27
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
VectorComponentPageTools
29.63% covered (danger)
29.63%
8 / 27
33.33% covered (danger)
33.33%
1 / 3
13.71
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getMenus
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 getTemplateData
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace MediaWiki\Skins\Vector\Components;
3
4use MediaWiki\Skins\Vector\Constants;
5use MediaWiki\Skins\Vector\FeatureManagement\FeatureManager;
6use MessageLocalizer;
7
8/**
9 * VectorComponentPageTools component
10 */
11class VectorComponentPageTools implements VectorComponent {
12
13    /** @var array */
14    private $menus;
15
16    /** @var MessageLocalizer */
17    private $localizer;
18
19    /** @var bool */
20    private $isPinned;
21
22    /** @var VectorComponentPinnableHeader */
23    private $pinnableHeader;
24
25    /** @var string */
26    public const ID = 'vector-page-tools';
27
28    /** @var string */
29    public const TOOLBOX_ID = 'p-tb';
30
31    /** @var string */
32    private const ACTIONS_ID = 'p-cactions';
33
34    /**
35     * @param array $menus
36     * @param MessageLocalizer $localizer
37     * @param FeatureManager $featureManager
38     */
39    public function __construct(
40        array $menus,
41        MessageLocalizer $localizer,
42        FeatureManager $featureManager
43    ) {
44        $this->menus = $menus;
45        $this->localizer = $localizer;
46        $this->isPinned = $featureManager->isFeatureEnabled( Constants::FEATURE_PAGE_TOOLS_PINNED );
47        $this->pinnableHeader = new VectorComponentPinnableHeader(
48            $localizer,
49            $this->isPinned,
50            // Name
51            self::ID,
52            // Feature name
53            'page-tools-pinned'
54        );
55    }
56
57    /**
58     * Revises the labels of the p-tb and p-cactions menus.
59     *
60     * @return array
61     */
62    private function getMenus(): array {
63        return array_map( function ( $menu ) {
64            switch ( $menu['id'] ?? '' ) {
65                case self::TOOLBOX_ID:
66                    $menu['label'] = $this->localizer->msg( 'vector-page-tools-general-label' )->text();
67                    break;
68                case self::ACTIONS_ID:
69                    $menu['label'] = $this->localizer->msg( 'vector-page-tools-actions-label' )->text();
70                    break;
71            }
72
73            return $menu;
74        }, $this->menus );
75    }
76
77    /**
78     * @inheritDoc
79     */
80    public function getTemplateData(): array {
81        $pinnedContainer = new VectorComponentPinnableContainer( self::ID, $this->isPinned );
82        $pinnableElement = new VectorComponentPinnableElement( self::ID );
83
84        $data = $pinnableElement->getTemplateData() +
85            $pinnedContainer->getTemplateData();
86
87        return $data + [
88            'data-pinnable-header' => $this->pinnableHeader->getTemplateData(),
89            'data-menus' => $this->getMenus()
90        ];
91    }
92}