• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

heimrichhannot / contao-utils-bundle / 11428825506

20 Oct 2024 06:56PM UTC coverage: 71.926% (+0.05%) from 71.88%
11428825506

push

github

koertho
fix another security class usage, fix tests

5 of 9 new or added lines in 1 file covered. (55.56%)

8 existing lines in 1 file now uncovered.

889 of 1236 relevant lines covered (71.93%)

3.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/src/EventListener/DcaField/DcaAuthorListener.php
1
<?php
2

3
namespace HeimrichHannot\UtilsBundle\EventListener\DcaField;
4

5
use Contao\BackendUser;
6
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
7
use Contao\DataContainer;
8
use Contao\FrontendUser;
9
use HeimrichHannot\UtilsBundle\Dca\AuthorField;
10
use HeimrichHannot\UtilsBundle\Dca\AuthorFieldConfiguration;
11
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
12

13
class DcaAuthorListener extends AbstractDcaFieldListener
14
{
15
    #[AsHook("loadDataContainer")]
16
    public function onLoadDataContainer(string $table): void
17
    {
18
        if (!isset(AuthorField::getRegistrations()[$table])) {
1✔
19
            return;
1✔
20
        }
21

22
        $options = AuthorField::getRegistrations()[$table];
1✔
23
        $authorFieldName = $this->getAuthorFieldName($options);
1✔
24

25
        /** @var TokenStorageInterface $tokenStorage */
26
        $tokenStorage = $this->container->get('token_storage');
1✔
27
        $user = $tokenStorage->getToken()?->getUser();
1✔
28

29
        $authorField = [
1✔
30
            'inputType' => 'select',
1✔
31
            'eval' => [
1✔
32
                'doNotCopy' => true,
1✔
33
                'mandatory' => true,
1✔
34
                'chosen' => true,
1✔
35
                'includeBlankOption' => true,
1✔
36
                'tl_class' => 'w50'
1✔
37
            ],
1✔
38
            'sql' => "int(10) unsigned NOT NULL default 0",
1✔
39
        ];
1✔
40

41
        $this->applyDefaultFieldAdjustments($authorField, $options);
1✔
42

43
        if ($options->isUseDefaultLabel()) {
1✔
44
            $authorField['label'] = &$GLOBALS['TL_LANG']['MSC']['utilsBundle']['author'];
1✔
45
        }
46

47
        $authorField['default'] = 0;
1✔
48
        if (AuthorField::TYPE_USER === $options->getType()) {
1✔
49
            if ($user instanceof BackendUser) {
1✔
UNCOV
50
                $authorField['default'] = $user->id;
×
51
            }
52
            $authorField['foreignKey'] = 'tl_user.name';
1✔
53
            $authorField['relation'] = ['type'=>'hasOne', 'load'=>'lazy'];
1✔
UNCOV
54
        } elseif (AuthorField::TYPE_MEMBER === $options->getType()) {
×
55
            if ($user instanceof FrontendUser) {
×
56
                $authorField['default'] = $user->id;
×
57
            }
UNCOV
58
            $authorField['foreignKey'] = "tl_member.CONCAT(firstname,' ',lastname)";
×
UNCOV
59
            $authorField['relation'] = ['type'=>'hasOne', 'load'=>'lazy'];
×
60
        }
61

62
        $GLOBALS['TL_DCA'][$table]['fields'][$authorFieldName] = $authorField;
1✔
63
        $GLOBALS['TL_DCA'][$table]['config']['oncopy_callback'][] = [self::class, 'onConfigCopyCallback'];
1✔
64
    }
65

66

67
    public function onConfigCopyCallback(int $insertId, DataContainer $dc): void
68
    {
69
        $options = AuthorField::getRegistrations()[$dc->table];
1✔
70
        $authorFieldName = $this->getAuthorFieldName($options);
1✔
71

72
        $model = $this->getModelInstance($dc->table, $insertId);
1✔
73
        if (!$model) {
1✔
UNCOV
74
            return;
×
75
        }
76

77
        /** @var TokenStorageInterface $tokenStorage */
78
        $tokenStorage = $this->container->get('token_storage');
1✔
79
        $user = $tokenStorage->getToken()?->getUser();
1✔
80

81
        $model->{$authorFieldName} = 0;
1✔
82

83
        if (AuthorField::TYPE_USER === $options->getType()) {
1✔
84
            if ($user instanceof BackendUser) {
1✔
NEW
85
                $model->{$authorFieldName} = $user->id;
×
86
            }
UNCOV
87
        } elseif (AuthorField::TYPE_MEMBER === $options->getType()) {
×
NEW
88
            if ($user instanceof FrontendUser) {
×
NEW
89
                $model->{$authorFieldName} = $user->id;
×
90
            }
91
        }
92
        $model->save();
1✔
93
    }
94

95
    /**
96
     * @param AuthorFieldConfiguration $options
97
     * @return string
98
     */
99
    protected function getAuthorFieldName(AuthorFieldConfiguration $options): string
100
    {
101
        if (!$options->hasFieldNamePrefix()) {
2✔
102
            return 'author';
2✔
103
        }
104
        if (str_ends_with($options->getFieldNamePrefix(), '_')) {
1✔
105
            return $options->getFieldNamePrefix() . 'author';
1✔
106
        } else {
107
            return $options->getFieldNamePrefix() . 'Author';
1✔
108
        }
109
    }
110

111
    public static function getSubscribedServices(): array
112
    {
UNCOV
113
        $services = parent::getSubscribedServices();
×
NEW
114
        $services['token_storage'] = TokenStorageInterface::class;
×
UNCOV
115
        return $services;
×
116
    }
117
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc