Completed
Pull Request — master (#51)
by De Cramer
02:42
created

WindowHelpFactory::setDataCollectionFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Core\Plugins\Gui;
4
5
use eXpansion\Framework\Core\DataProviders\ChatCommandDataProvider;
6
use eXpansion\Framework\Core\Model\Gui\Grid\DataCollectionFactory;
7
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilder;
8
use eXpansion\Framework\Core\Model\Gui\Grid\GridBuilderFactory;
9
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
10
use eXpansion\Framework\Core\Services\ChatCommands;
11
use FML\Controls\Frame;
12
use FML\Controls\Label;
13
use FML\Controls\Quads\Quad_Icons64x64_1;
14
15
16
/**
17
 * Class HelpFactory
18
 *
19
 * @package eXpansion\Framework\AdminGroups\Plugins\Window;
20
 * @author  oliver de Cramer <[email protected]>
21
 */
22
class WindowHelpFactory extends WindowFactory
23
{
24
    /** @var GridBuilderFactory */
25
    protected $gridBuilderFactory;
26
27
    /** @var DataCollectionFactory */
28
    protected $dataCollectionFactory;
29
30
    /** @var ChatCommands */
31
    protected $chatCommands;
32
33
    /** @var ChatCommandDataProvider */
34
    protected $chatCommandDataPovider;
35
36
    /**
37
     * @param GridBuilderFactory $gridBuilderFactory
38
     */
39
    public function setGridBuilderFactory($gridBuilderFactory)
40
    {
41
        $this->gridBuilderFactory = $gridBuilderFactory;
42
    }
43
44
    /**
45
     * @param DataCollectionFactory $dataCollectionFactory
46
     */
47
    public function setDataCollectionFactory($dataCollectionFactory)
48
    {
49
        $this->dataCollectionFactory = $dataCollectionFactory;
50
    }
51
52
    /**
53
     * @param ChatCommands $chatCommands
54
     */
55
    public function setChatCommands($chatCommands)
56
    {
57
        $this->chatCommands = $chatCommands;
58
    }
59
60
    public function setChatCommandDataProvide($chatCommandDataPovider)
61
    {
62
        $this->chatCommandDataPovider = $chatCommandDataPovider;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    protected function createContent(ManialinkInterface $manialink)
69
    {
70
        $collection = $this->dataCollectionFactory->create($this->chatCommands->getChatCommands());
71
        $collection->setPageSize(2);
72
73
        $helpButton = new Label();
74
        $helpButton->setText('')
75
            ->setSize(6, 6)
76
            ->setAreaColor("0000")
77
            ->setAreaFocusColor("0000");
78
79
        $gridBuilder = $this->gridBuilderFactory->create();
80
        $gridBuilder->setManialink($manialink)
81
            ->setDataCollection($collection)
82
            ->setManialinkFactory($this)
83
            ->addTextColumn(
84
                'command',
85
                "expansion_core.windows.chat_commands.column_command",
86
                25
87
            )
88
            ->addTextColumn(
89
                'description',
90
                'expansion_core.windows.chat_commands.column_description',
91
                70,
92
                false,
93
                true
94
            )
95
            ->addActionColumn('help', '', 5, array($this, 'callbackHelp'), $helpButton);
96
97
        $manialink->setData('grid', $gridBuilder);
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103
    protected function updateContent(ManialinkInterface $manialink)
104
    {
105
        /** @var Frame $contentFrame */
106
        $contentFrame = $manialink->getContentFrame();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eXpansion\Framework\Core...\Gui\ManialinkInterface as the method getContentFrame() does only exist in the following implementations of said interface: eXpansion\Framework\Core\Model\Gui\Window.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
107
        $contentFrame->removeAllChildren();
108
109
        $collection = $this->dataCollectionFactory->create($this->chatCommands->getChatCommands());
110
        $collection->setPageSize(20);
111
112
        /** @var GridBuilder $gridBuilder */
113
        $gridBuilder = $manialink->getData('grid');
114
        $contentFrame->addChild($gridBuilder->build($contentFrame->getWidth(), $contentFrame->getHeight()));
115
    }
116
117
    /**
118
     * Callbacked called when help button is pressed.
119
     *
120
     * @param $login
121
     * @param $params
122
     * @param $arguments
123
     */
124
    public function callbackHelp($login, $params, $arguments)
125
    {
126
        $this->chatCommandDataPovider->onPlayerChat(0, $login, '/' . $arguments['command'] . ' -h', true);
127
    }
128
}
129