Issues (6)

src/Entity/Activity.php (2 issues)

1
<?php
2
declare(strict_types=1);
3
4
namespace Basster\ElasticaLoggable\Entity;
5
6
use Elastica\ArrayableInterface;
7
use Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry;
8
9
/**
10
 * Class Activity.
11
 */
12
class Activity extends AbstractLogEntry implements ArrayableInterface
13
{
14
    private const OLD_INDEX = 0;
15
16
    /** @var array */
17
    private $changeSet;
18
19
    public function __construct(string $action, ?string $username, string $objectClass)
20
    {
21
        $this->setAction($action);
22
        $this->setUsername($username);
23
        $this->setObjectClass($objectClass);
24
        $this->setLoggedAt();
25
    }
26
27
    public function toArray(): array
28
    {
29
        return [
30
            'action' => $this->getAction(),
31
            'logged_at' => $this->getLoggedAt()->format(\DateTime::ATOM),
32
            'object_id' => $this->getObjectId(),
33
            'object_class' => $this->getObjectClass(),
34
            'data' => $this->getData(),
35
            'username' => $this->getUsername(),
36
        ];
37
    }
38
39
    public function getData()
40
    {
41
        $changed = parent::getData();
42
        $data = [];
43
44
        if ($changed) { // is null during deletions
0 ignored issues
show
Bug Best Practice introduced by
The expression $changed of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
The condition $changed can never be true.
Loading history...
45
            foreach ($changed as $property => $value) {
46
                $data[$property] = [
47
                    'from' => null,
48
                    'to' => $value,
49
                ];
50
51
                if (array_key_exists($property, $this->changeSet)) {
52
                    $data[$property]['from'] = $this->changeSet[$property][self::OLD_INDEX];
53
                }
54
            }
55
        }
56
57
        return $data;
58
    }
59
60
    public function setChangeSet($changeSet)
61
    {
62
        $this->changeSet = $changeSet;
63
    }
64
}
65