UserRegistrationPage::requireDefaultRecords()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 2
nop 0
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 3
rs 9.9332
c 0
b 0
f 0
1
<?php
2
namespace UserManagement\Page;
3
4
use Page;
0 ignored issues
show
Bug introduced by
The type Page 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...
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\ORM\DB;
7
8
/**
9
 * Class UserRegistrationPage
10
 *
11
 * @package user-management
12
 */
13
class UserRegistrationPage extends Page
14
{
15
16 1
    public function canCreate($member = null, $context = array())
17
    {
18 1
        return !self::get()->exists();
19
    }
20
21
    /**
22
     * Returns the link or the URLSegment to the user registration page on this site
23
     *
24
     * @param boolean $urlSegment Return the URLSegment only
25
     *
26
     * @return mixed
27
     */
28 1
    public static function find_link($urlSegment = false)
29
    {
30 1
        $page = self::get_if_registration_page_exists();
31 1
        return ($urlSegment) ? $page->URLSegment : $page->Link();
32
    }
33
    
34
    /**
35
     * @return object
36
     */
37 1
    protected static function get_if_registration_page_exists()
38
    {
39 1
        if ($page = DataObject::get_one(self::class)) {
40 1
            return $page;
41
        }
42
        user_error(_t(__CLASS__ . '.NoPage', 'No UserRegistrationPage was found. 
43
            Please create one in the CMS!'), E_USER_ERROR);
44
        return null; // just to keep static analysis happy
45
    }
46
47
    /**
48
     * This module always requires a page model.
49
     */
50 1
    public function requireDefaultRecords()
51
    {
52 1
        parent::requireDefaultRecords();
53 1
        if (!self::get()->exists() && $this->config()->create_default_pages) {
54
            /**
55
             * @var UserRegistrationPage $page
56
             */
57 1
            $page = self::create()->update(
58
                [
59 1
                    'Title' => 'UserRegistration',
60 1
                    'URLSegment' => UserRegistrationPageController::config()->url_segment,
61 1
                    'ShowInMenus' => 0,
62
                ]
63
            );
64 1
            $page->write();
65 1
            $page->publishSingle();
66 1
            $page->flushCache();
67 1
            DB::alteration_message('UserRegistration page created', 'created');
68
        }
69
    }
70
}
71