UserProfilePage::find_link()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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