Factory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
namespace PortlandLabs\Slackbot\Slack\Rtm\Event;
3
4
use PortlandLabs\Slackbot\Slack\Rtm\Event\Message;
5
use Psr\Container\ContainerInterface;
6
use Psr\Log\LoggerAwareTrait;
7
use Psr\Log\LoggerInterface;
8
9
class Factory
10
{
11
12
    use LoggerAwareTrait;
13
14
    /**
15
     * A map of types this factory can build
16
     */
17
    private const TYPES = [
18
        'hello' => Hello::class,
19
        'pong' => Pong::class,
20
        'message' => Message\UserMessage::class,
21
    ];
22
23
    private const SHORT_MAP = [
24
        'ts' => 'timestamp'
25
    ];
26
27
    /** @var ContainerInterface */
28
    protected $container;
29
30
    public function __construct(ContainerInterface $container, LoggerInterface $logger)
31
    {
32
        $this->container = $container;
33
        $this->setLogger($logger);
34
    }
35
36
    /**
37
     * Generate a new Event instance based on message data
38
     *
39
     * @param array $data
40
     *
41
     * @return Event|null
0 ignored issues
show
Bug introduced by
The type PortlandLabs\Slackbot\Slack\Rtm\Event\Event was not found. Did you mean Event? If so, make sure to prefix the type with \.
Loading history...
42
     */
43
    public function buildEvent(array $data)
44
    {
45
        // If the type isn't set, this likely isn't an event at all
46
        if (!isset($data['type'])) {
47
            return null;
48
        }
49
50
        // Find the type class
51
        $typeKey = $this->getMapKey($data);
52
        $typeClass = self::TYPES[$typeKey] ?? null;
53
54
        // If we can't build it, log and return null
55
        if (!$typeClass) {
56
            $this->logger->debug(sprintf('[<bold>RTM.FTY</bold>] Missing event class for type "%s" "%s"', $typeKey, $typeClass));
0 ignored issues
show
Bug introduced by
The method debug() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
            $this->logger->/** @scrutinizer ignore-call */ 
57
                           debug(sprintf('[<bold>RTM.FTY</bold>] Missing event class for type "%s" "%s"', $typeKey, $typeClass));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
            return null;
58
        }
59
60
        // Build the event
61
        $event = $this->container->get($typeClass);
62
        unset($data['type'], $data['subtype']);
63
64
        if ($event instanceof Pong) {
65
            $event->setData($data);
66
        }
67
68
        foreach ($data as $key => $value) {
69
            $key = self::SHORT_MAP[$key] ?? $key;
70
            $setter = camel_case('set_' . $key);
0 ignored issues
show
Deprecated Code introduced by
The function camel_case() has been deprecated: Str::camel() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

70
            $setter = /** @scrutinizer ignore-deprecated */ camel_case('set_' . $key);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
71
72
            if (!method_exists($event, $setter)) {
73
                $this->logger->debug(sprintf('[<bold>RTM.FTY</bold>] Missing event setter for "%s" data "%s"', $typeKey, $key));
74
                continue;
75
            }
76
77
            $event->{$setter}($value);
78
        }
79
80
        return $event;
81
    }
82
83
    /**
84
     * Get a key for the type as specified by the data array
85
     *
86
     * @example input ['type' => 'foo', 'subtype' => 'bar'], output 'foo.bar'. If the subtype were omitted output would just be 'foo'
87
     *
88
     * @param array $data
89
     *
90
     * @return string
91
     */
92
    protected function getMapKey(array $data)
93
    {
94
        $type = $data['type'] ?? '';
95
        $subtype = $data['subtype'] ?? null;
96
97
        // Return type.subtype or type
98
        return $type . ($subtype ? '.' . $subtype : '');
99
    }
100
101
}