Completed
Push — master ( 5c2226...1e1b69 )
by Peter
6s
created

ReleaseNotification::prepConfig()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 6
eloc 8
nc 12
nop 2
1
<?php
2
3
/**
4
 * I needed something that sends emails on dev/build. Later it became this release notificator.
5
 *
6
 * @author [email protected]
7
 */
8
9
class ReleaseNotification extends DataObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    /**
12
     * @var array
13
     */
14
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15
        'Changelog' => 'Text',
16
    );
17
18
    /**
19
     * We are abusing requireDefaultRecords to run this on dev/build.
20
     */
21
    public function requireDefaultRecords()
22
    {
23
        parent::requireDefaultRecords();
24
25
        // verbose messages
26
        DB::alteration_message('Searching for environment configuration for ' . Director::absoluteURL('/'), 'created');
27
28
        // get the config based on the URL (URL is key)
29
        $config = $this->prepConfig($this->config()->get('environments'), Director::absoluteURL('/'));
0 ignored issues
show
Security Bug introduced by
It seems like \Director::absoluteURL('/') targeting Director::absoluteURL() can also be of type false; however, ReleaseNotification::prepConfig() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
30
31
        // only run if everything is fine
32
        if (!is_array($config) || empty($config)) {
33
            DB::alteration_message('No configuration found.', 'created');
34
        } else if (!file_exists('../' . $config['filename'])) {
35
            DB::alteration_message('No CHANGELOG.md-file found.', 'created');
36
        } else {
37
            DB::alteration_message($config['environment_name'] . ' identified.', 'created');
38
39
            // get the information in the database for the last release notification
40
            $record = (self::get()->count() == 0) ? new self() : self::get()->first();
41
42
            // load the changelog and prep it by removing the first few lines
43
            $changelog = trim(preg_replace('/^(.*\n){4}/', '', file_get_contents('../' . $config['filename'])));
44
45
            // check if the CHANGELOG.md file has been changed since the last run
46
            if ($changelog == '') {
47
                DB::alteration_message('No change identified.', 'created');
48
            } else if (md5($changelog) != md5($record->Changelog)) {
49
                // email the changelog out
50
                foreach ($config['recipients'] as $recipient) {
51
                    Email::create(
52
                        $config['from'],
53
                        $recipient,
54
                        $config['subject'],
55
56
                        // remove the old part and add more useful information to the content
57
                        sprintf(
58
                            "%s (%s)\n\n%s",
59
                            $config['environment_name'],
60
                            $config['url'],
61
                            $changelog
62
                        )
63
                    )->sendPlain();
64
65
                    DB::alteration_message($recipient . ' notified', 'created');
66
                }
67
68
                // save the new changelog to ensure we aren't re-running this in the next step
69
                $record->Changelog = $changelog;
70
            }
71
72
            // say welcome :)
73
            if (!$record->ID) {
74
                DB::alteration_message('Install of FriendsOfSilverStripe/release-notifications');
75
            }
76
77
            $record->write();
78
        }
79
    }
80
81
    /**
82
     * sorts the config a bit different and returns it.
83
     *
84
     * @param array $environments
85
     * @param string $url
86
     *
87
     * @return array (either configuration or empty)
88
     */
89
    public function prepConfig($environments, $url)
90
    {
91
        $environmentsByURL = array();
92
        if (!is_array($environments)) {
93
            $environments = array();
94
        }
95
96
        foreach ($environments as $environment) {
97
            if (is_array($environment) && array_key_exists('url', $environment)) {
98
                $environmentsByURL[$environment['url']] = $environment;
99
            }
100
        }
101
102
        return array_key_exists($url, $environmentsByURL) ? $environmentsByURL[$url] : [];
103
    }
104
}
105