GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( c01944...a8a408 )
by Bob Olde
7s
created

Export_EntryService::getAttributes()   C

Complexity

Conditions 11
Paths 108

Size

Total Lines 37
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 11

Importance

Changes 9
Bugs 4 Features 3
Metric Value
c 9
b 4
f 3
dl 0
loc 37
ccs 20
cts 20
cp 1
rs 5.1372
cc 11
eloc 16
nc 108
nop 2
crap 11

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Craft;
4
5
/**
6
 * Export Entry Service.
7
 *
8
 * Handles exporting entries.
9
 *
10
 * @author    Bob Olde Hampsink <[email protected]>
11
 * @copyright Copyright (c) 2015, Bob Olde Hampsink
12
 * @license   MIT
13
 *
14
 * @link      http://github.com/boboldehampsink
15
 */
16
class Export_EntryService extends BaseApplicationComponent implements IExportElementType
17
{
18
    /**
19
     * Return export template.
20
     *
21
     * @return string
22
     */
23 1
    public function getTemplate()
24
    {
25 1
        return 'export/sources/_entry';
26
    }
27
28
    /**
29
     * Get entry sections.
30
     *
31
     * @return array|bool
32
     */
33 1
    public function getGroups()
34
    {
35
        // Get editable sections for user
36 1
        $editable = craft()->sections->getEditableSections();
37
38
        // Get sections but not singles
39 1
        $sections = array();
40 1
        foreach ($editable as $section) {
41 1
            if ($section->type != SectionType::Single) {
42 1
                $sections[] = $section;
43 1
            }
44 1
        }
45
46 1
        return $sections;
47
    }
48
49
    /**
50
     * Return entry fields.
51
     *
52
     * @param array $settings
53
     * @param bool  $reset
54
     *
55
     * @return array
56
     */
57 2
    public function getFields(array $settings, $reset)
58
    {
59
        // Set criteria
60 2
        $criteria = new \CDbCriteria();
61 2
        $criteria->condition = 'settings = :settings';
62 2
        $criteria->params = array(
63 2
            ':settings' => JsonHelper::encode($settings),
64
        );
65
66
        // Check if we have a map already
67 2
        $stored = craft()->export->findMap($criteria);
68
69 2
        if (!count($stored) || $reset) {
70
71
            // Get section id
72 1
            $section = $settings['elementvars']['section'];
73
74
            // Get entrytype id(s)
75 1
            $entrytype = $settings['elementvars']['entrytype'];
76
77
            // If "All"
78 1
            $entrytypes = empty($entrytype) ? craft()->sections->getEntryTypesBySectionId($section) : array(craft()->sections->getEntryTypeById($entrytype));
79
80
            // Create a nice field map
81 1
            $fields = array();
82
83
            // With multiple or one entry type
84 1
            foreach ($entrytypes as $entrytype) {
85
86
                // Set the static fields for this type
87
                $layout = array(
88 1
                    ExportModel::HandleId => array('name' => Craft::t('ID'), 'checked' => 0),
89 1
                    ExportModel::HandleTitle.'_'.$entrytype->id => array('name' => $entrytype->hasTitleField ? $entrytype->titleLabel : Craft::t('Title'), 'checked' => 1, 'entrytype' => $entrytype->id),
90 1
                    ExportModel::HandleSlug => array('name' => Craft::t('Slug'), 'checked' => 0),
91 1
                    ExportModel::HandleParent => array('name' => Craft::t('Parent'), 'checked' => 0),
92 1
                    ExportModel::HandleAncestors => array('name' => Craft::t('Ancestors'), 'checked' => 0),
93 1
                    ExportModel::HandleAuthor => array('name' => Craft::t('Author'), 'checked' => 0),
94 1
                    ExportModel::HandlePostDate => array('name' => Craft::t('Post Date'), 'checked' => 0),
95 1
                    ExportModel::HandleExpiryDate => array('name' => Craft::t('Expiry Date'), 'checked' => 0),
96 1
                    ExportModel::HandleEnabled => array('name' => Craft::t('Enabled'), 'checked' => 0),
97 1
                    ExportModel::HandleStatus => array('name' => Craft::t('Status'), 'checked' => 0),
98 1
                );
99
100
                // Set the dynamic fields for this type
101 1
                $tabs = craft()->fields->getLayoutById($entrytype->fieldLayoutId)->getTabs();
102 1
                foreach ($tabs as $tab) {
103 1
                    $fieldData = array();
104 1
                    foreach ($tab->getFields() as $field) {
105 1
                        $data = $field->getField();
106 1
                        $fieldData[$data->handle] = array('name' => $data->name, 'checked' => 1, 'fieldtype' => $data->type);
107 1
                    }
108 1
                    $layout += $fieldData;
109 1
                }
110
111
                // Set the static fields also
112 1
                $fields += $layout;
113 1
            }
114 1
        } else {
115
116
            // Get the stored map
117 1
            $fields = $stored->map;
118
        }
119
120
        // Return fields
121 2
        return $fields;
122
    }
123
124
    /**
125
     * Set entry criteria.
126
     *
127
     * @param array $settings
128
     *
129
     * @return ElementCriteriaModel
130
     */
131 1
    public function setCriteria(array $settings)
132
    {
133
134
        // Get entries by criteria
135 1
        $criteria = craft()->elements->getCriteria(ElementType::Entry);
136 1
        $criteria->order = 'id '.$settings['sort'];
137 1
        $criteria->offset = $settings['offset'];
138 1
        $criteria->limit = $settings['limit'];
139 1
        $criteria->status = isset($settings['map']['status']) ? $settings['map']['status'] : null;
140
141
        // Get by section and entrytype
142 1
        $criteria->sectionId = $settings['elementvars']['section'];
143 1
        $criteria->type = $settings['elementvars']['entrytype'];
144
145 1
        return $criteria;
146
    }
147
148
    /**
149
     * Get entry attributes.
150
     *
151
     * @param array            $map
152
     * @param BaseElementModel $element
153
     *
154
     * @return array
155
     */
156 1
    public function getAttributes(array $map, BaseElementModel $element)
157
    {
158 1
        $attributes = array();
159
160
        // Try to parse checked fields through prepValue
161 1
        foreach ($map as $handle => $data) {
162 1
            if ($data['checked'] && !strstr($handle, ExportModel::HandleTitle)) {
163
                try {
164 1
                    $attributes[$handle] = $element->$handle;
165 1
                } catch (\Exception $e) {
166 1
                    $attributes[$handle] = null;
167
                }
168 1
            }
169 1
        }
170
171
        // Title placeholder for all element types
172 1
        foreach (craft()->sections->getEntryTypesBySectionId($element->sectionId) as $entrytype) {
173
174
            // Set title
175 1
            $attributes[ExportModel::HandleTitle.'_'.$entrytype->id] = $entrytype->id == $element->typeId ? $element->{ExportModel::HandleTitle} : '';
176 1
        }
177
178
        // Get parent for structures
179 1
        if (array_key_exists(ExportModel::HandleParent, $map)) {
180 1
            $attributes[ExportModel::HandleParent] = $element->getAncestors() ? $element->getAncestors(1)->first() : '';
181 1
        }
182
183
        // Get ancestors for structures
184 1
        if (array_key_exists(ExportModel::HandleAncestors, $map)) {
185 1
            $attributes[ExportModel::HandleAncestors] = $element->getAncestors() ? implode('/', $element->getAncestors()->find()) : '';
186 1
        }
187
188
        // Call hook allowing 3rd-party plugins to modify attributes
189 1
        craft()->plugins->call('modifyExportAttributes', array(&$attributes, $element));
190
191 1
        return $attributes;
192
    }
193
}
194