ChainMail   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 84.62%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 114
ccs 22
cts 26
cp 0.8462
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A renderSection() 0 7 1
A getUsedSections() 0 7 1
A renderSections() 0 10 2
A createMail() 0 9 1
A __construct() 0 16 2
1
<?php
2
/**
3
 * BrightNucleus ChainMail Component.
4
 *
5
 * @package   BrightNucleus/ChainMail
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   MIT
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\ChainMail;
13
14
use BrightNucleus\ChainMail\Support\Factory;
15
use BrightNucleus\Config\ConfigFactory;
16
use BrightNucleus\Config\ConfigInterface;
17
18
/**
19
 * Class ChainMail.
20
 *
21
 * @since   1.0.0
22
 *
23
 * @package BrightNucleus\ChainMail
24
 * @author  Alain Schlesser <[email protected]>
25
 */
26
class ChainMail
27
{
28
29
    const DEFAULT_CONFIG = __DIR__ . '/../config/defaults.php';
30
31
    /**
32
     * Configuration Settings.
33
     *
34
     * @since 1.0.0
35
     *
36
     * @var ConfigInterface
37
     */
38
    protected $config;
39
40
    /**
41
     * Instantiate a ChainMail object.
42
     *
43
     * @since 1.0.0
44
     *
45
     * @param ConfigInterface|null $config Optional. Configuration settings.
46
     */
47 16
    public function __construct(ConfigInterface $config = null)
48
    {
49
50 16
        $defaults = ConfigFactory::create(include(self::DEFAULT_CONFIG));
51
52 16
        if ( ! $config) {
53 16
            $this->config = $defaults;
54
55 16
            return;
56
        }
57
58
        $this->config = ConfigFactory::create(array_replace_recursive(
59
                (array)$defaults,
60
                (array)$config)
61
        );
62
    }
63
64
    /**
65
     * Render a specific section.
66
     *
67
     * @since 1.0.0
68
     *
69
     * @param string $sectionType Type of section to render.
70
     * @param array  $context     The context in which to render the section.
71
     *
72
     * @return string Rendered HTML.
73
     */
74 8
    public static function renderSection($sectionType, array $context)
75
    {
76
        /** @var Section $section */
77 8
        $section = $context['sections'][$sectionType];
78
79 8
        return $section->render($context);
80
    }
81
82
    /**
83
     * Get an array of strings representing the sections that are used by the
84
     * template.
85
     *
86
     * @since 1.0.0
87
     *
88
     * @param array $context The context in which to render the section.
89
     *
90
     * @return array Array of strings with section types.
91
     */
92 8
    public static function getUsedSections(array $context)
93
    {
94
        /** @var Template $template */
95 8
        $template = $context['template'];
96
97 8
        return $template->getUsedSections();
98
    }
99
100
    /**
101
     * Render all used sections.
102
     *
103
     * @since 1.0.0
104
     *
105
     * @param array $context The context in which to render the section.
106
     *
107
     * @return string Rendered HTML.
108
     */
109 8
    public static function renderSections(array $context)
110
    {
111 8
        $output = '';
112
113 8
        foreach (self::getUsedSections($context) as $section) {
114 8
            $output .= self::renderSection($section, $context);
115
        }
116
117 8
        return $output;
118
    }
119
120
    /**
121
     * Create a new mail object.
122
     *
123
     * @since 1.0.0
124
     *
125
     * @param string          $format   Optional. Format to use.
126
     * @param string|Template $template Optional. Template to be used.
127
     *
128
     * @return Mail
129
     */
130 16
    public function createMail($format = 'html', $template = 'BasicTemplate')
131
    {
132 16
        $mail_factory = new Factory($this->config, 'mails');
133 16
        $mail_class   = $this->config->getKey('formats')[$format]['mail'];
134 16
        $mail         = $mail_factory->create($mail_class);
135 16
        $mail->setTemplate($template);
136
137 16
        return $mail;
138
    }
139
}
140