HistoryControllerFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 15
c 3
b 0
f 0
dl 0
loc 41
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isEnabled() 0 4 2
A create() 0 24 6
1
<?php
2
3
namespace SilverStripe\VersionedAdmin\Controllers;
4
5
use SilverStripe\CMS\Controllers\CMSPageHistoryController;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Control...MSPageHistoryController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Core\Extensible;
9
use SilverStripe\Core\Injector\Factory;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Versioned\Versioned;
12
13
/**
14
 * The history controller factory decides which CMS history controller to use, out of the default from the
15
 * silverstripe/cms module or the history viewer controller from this module, depending on the current page type
16
 *
17
 * @deprecated 1.1.0:2.0.0
18
 */
19
class HistoryControllerFactory implements Factory
20
{
21
    use Extensible;
22
23
    public function create($service, array $params = array())
24
    {
25
        // If no request is available yet, return the default controller
26
        if (Injector::inst()->has(HTTPRequest::class)) {
27
            $request = Injector::inst()->get(HTTPRequest::class);
28
            $id = $request->param('ID');
29
30
            if ($id && is_numeric($id)) {
31
                // Ensure we read from the draft stage at this position
32
                $page = Versioned::get_one_by_stage(
33
                    SiteTree::class,
34
                    Versioned::DRAFT,
35
                    sprintf('"SiteTree"."ID" = \'%d\'', $id)
36
                );
37
38
                if ($page && !$this->isEnabled($page)) {
39
                    // Injector is not used to prevent an infinite loop
40
                    return new CMSPageHistoryViewerController();
41
                }
42
            }
43
        }
44
45
        // Injector is not used to prevent an infinite loop
46
        return Injector::inst()->create(CMSPageHistoryViewerController::class);
47
    }
48
49
    /**
50
     * Only deactivate for pages that have a history viewer capability removed. Extensions can provide their
51
     * own two cents about this criteria.
52
     *
53
     * @param SiteTree $record
54
     * @return bool
55
     */
56
    public function isEnabled(SiteTree $record)
57
    {
58
        $enabledResults = $this->extend('updateIsEnabled', $record);
59
        return (empty($enabledResults) || min($enabledResults) !== false);
60
    }
61
}
62