Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.96% covered (success)
86.96%
20 / 23
80.00% covered (success)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
MetaData
86.96% covered (success)
86.96%
20 / 23
80.00% covered (success)
80.00%
4 / 5
6.08
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setFaqId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setFaqLanguage
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setCategories
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 save
81.25% covered (success)
81.25%
13 / 16
0.00% covered (danger)
0.00%
0 / 1
2.03
1<?php
2
3/**
4 * FAQ meta-data class for phpMyFAQ.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2020-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2020-11-04
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Faq;
21
22use phpMyFAQ\Category;
23use phpMyFAQ\Category\Permission as CategoryPermission;
24use phpMyFAQ\Category\Relation;
25use phpMyFAQ\Configuration;
26use phpMyFAQ\Faq\Permission as FaqPermission;
27use phpMyFAQ\Visits;
28
29/**
30 * Class FaqMetaData
31 *
32 * @package phpMyFAQ\Faq
33 */
34class MetaData
35{
36    private ?int $faqId = null;
37
38    private ?string $faqLanguage = null;
39
40    /** @var int[]|null */
41    private ?array $categories = null;
42
43    /**
44     * FaqPermission constructor.
45     */
46    public function __construct(
47        private readonly Configuration $configuration,
48    ) {
49    }
50
51    public function setFaqId(int $faqId): MetaData
52    {
53        $this->faqId = $faqId;
54        return $this;
55    }
56
57    public function setFaqLanguage(string $faqLanguage): MetaData
58    {
59        $this->faqLanguage = $faqLanguage;
60        return $this;
61    }
62
63    /**
64     * @param int[] $categories
65     */
66    public function setCategories(array $categories): MetaData
67    {
68        $this->categories = $categories;
69        return $this;
70    }
71
72    /**
73     * This method saves the category relations, the initial visits
74     * and the permissions
75     */
76    public function save(): void
77    {
78        $categories = $this->categories ?? [];
79        $faqId = $this->faqId ?? 0;
80        $faqLanguage = $this->faqLanguage ?? '';
81
82        $categoryRelation = new Relation($this->configuration, new Category($this->configuration));
83        $categoryRelation->add($categories, $faqId, $faqLanguage);
84
85        // Activate visits
86        $visits = new Visits($this->configuration);
87        $visits->logViews($faqId);
88
89        // Set permissions: derive from category permissions and apply to both FAQ record and categories
90        $faqPermission = new FaqPermission($this->configuration);
91        $categoryPermission = new CategoryPermission($this->configuration);
92
93        $userPermissions = $categoryPermission->get(CategoryPermission::USER, $categories);
94
95        // Apply user permissions to the FAQ record and keep category permissions aligned
96        $faqPermission->add(FaqPermission::USER, $faqId, $userPermissions);
97        $categoryPermission->add(CategoryPermission::USER, $categories, $userPermissions);
98
99        if ($this->configuration->get(item: 'security.permLevel') !== 'basic') {
100            $groupPermissions = $categoryPermission->get(CategoryPermission::GROUP, $categories);
101            $faqPermission->add(FaqPermission::GROUP, $faqId, $groupPermissions);
102            $categoryPermission->add(CategoryPermission::GROUP, $categories, $groupPermissions);
103        }
104    }
105}