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

WindowHelpFactory::updateContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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\Quads\Quad_Icons64x64_1;
13
14
15
/**
16
 * Class HelpFactory
17
 *
18
 * @package eXpansion\Framework\AdminGroups\Plugins\Window;
19
 * @author  oliver de Cramer <[email protected]>
20
 */
21
class WindowHelpFactory extends WindowFactory
22
{
23
    /** @var GridBuilderFactory  */
24
    protected $gridBuilderFactory;
25
26
    /** @var DataCollectionFactory  */
27
    protected $dataCollectionFactory;
28
29
    /** @var ChatCommands  */
30
    protected $chatCommands;
31
32
    /** @var ChatCommandDataProvider  */
33
    protected $chatCommandDataPovider;
34
35
    /**
36
     * @param GridBuilderFactory $gridBuilderFactory
37
     */
38
    public function setGridBuilderFactory($gridBuilderFactory)
39
    {
40
        $this->gridBuilderFactory = $gridBuilderFactory;
41
    }
42
43
    /**
44
     * @param DataCollectionFactory $dataCollectionFactory
45
     */
46
    public function setDataCollectionFactory($dataCollectionFactory)
47
    {
48
        $this->dataCollectionFactory = $dataCollectionFactory;
49
    }
50
51
    /**
52
     * @param ChatCommands $chatCommands
53
     */
54
    public function setChatCommands($chatCommands)
55
    {
56
        $this->chatCommands = $chatCommands;
57
    }
58
59
    public function setChatCommandDataProvide($chatCommandDataPovider)
60
    {
61
        $this->chatCommandDataPovider = $chatCommandDataPovider;
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    protected function createContent(ManialinkInterface $manialink)
68
    {
69
        $collection = $this->dataCollectionFactory->create($this->chatCommands->getChatCommands());
70
        $collection->setPageSize(2);
71
72
        $helpButton = Quad_Icons64x64_1::create();
73
        $helpButton->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_TrackInfo);
74
        $helpButton->setSize(6,6);
75
76
        $gridBuilder = $this->gridBuilderFactory->create();
77
        $gridBuilder->setManialink($manialink)
78
            ->setDataCollection($collection)
79
            ->setManialinkFactory($this)
80
            ->addTextColumn('command', "Command", 25)
81
            ->addTextColumn('description', 'Description', 70)
82
            ->addActionColumn('help', '', 5, array($this, 'callbackHelp'), $helpButton);
83
84
        $manialink->setData('grid', $gridBuilder);
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    protected function updateContent(ManialinkInterface $manialink)
91
    {
92
        /** @var Frame $contentFrame */
93
        $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...
94
        $contentFrame->removeAllChildren();
95
96
        $collection = $this->dataCollectionFactory->create($this->chatCommands->getChatCommands());
97
        $collection->setPageSize(20);
98
99
        /** @var GridBuilder $gridBuilder */
100
        $gridBuilder = $manialink->getData('grid');
101
        $contentFrame->addChild($gridBuilder->build($contentFrame->getWidth(), $contentFrame->getHeight()));
102
    }
103
104
    /**
105
     * Callbacked called when help button is pressed.
106
     *
107
     * @param $login
108
     * @param $params
109
     * @param $arguments
110
     */
111
    public function callbackHelp($login, $params, $arguments)
112
    {
113
        $this->chatCommandDataPovider->onPlayerChat(0, $login, '/' . $arguments['command'] . ' -h', true);
114
    }
115
}