Passed
Branch master (6f4de6)
by Sathish
01:37
created

UserRegistrationPage::requireDefaultRecords()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 2
nop 0
dl 0
loc 18
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
    private static $table_name = 'UserManagement_UserRegistrationPage';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
17
    
18
    public function canCreate($member = null, $context = array())
19
    {
20
        return !self::get()->exists();
21
    }
22
23
    /**
24
     * Returns the link or the URLSegment to the user registration page on this site
25
     *
26
     * @param boolean $urlSegment Return the URLSegment only
27
     *
28
     * @return mixed
29
     */
30
    public static function find_link($urlSegment = false)
31
    {
32
        $page = self::get_if_registration_page_exists();
33
        return ($urlSegment) ? $page->URLSegment : $page->Link();
34
    }
35
    
36
    /**
37
     * @return UserRegistrationPage
38
     */
39
    protected static function get_if_registration_page_exists()
40
    {
41
        if ($page = DataObject::get_one(self::class)) {
42
            return $page;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $page returns the type SilverStripe\ORM\DataObject which is incompatible with the documented return type UserManagement\Page\UserRegistrationPage.
Loading history...
43
        }
44
        user_error(_t(__CLASS__ . '.NoPage', 'No UserRegistrationPage was found. 
45
            Please create one in the CMS!'), E_USER_ERROR);
46
        return null; // just to keep static analysis happy
47
    }
48
49
    /**
50
     * This module always requires a page model.
51
     */
52
    public function requireDefaultRecords()
53
    {
54
        parent::requireDefaultRecords();
55
        if (!self::get()->exists() && $this->config()->create_default_pages) {
56
            /**
57
             * @var UserRegistrationPage $page
58
             */
59
            $page = self::create()->update(
60
                [
61
                'Title' => 'UserRegistration',
62
                'URLSegment' => UserRegistrationPageController::config()->url_segment,
63
                'ShowInMenus' => 0,
64
                ]
65
            );
66
            $page->write();
67
            $page->publishSingle();
68
            $page->flushCache();
69
            DB::alteration_message('UserRegistration page created', 'created');
70
        }
71
    }
72
}
73