Completed
Push — master ( 2825b9...504b57 )
by satoru
07:25 queued 05:25
created

ContentsFileBehavior::fileDelete()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 11
nc 4
nop 2
1
<?php
2
3
namespace ContentsFile\Model\Behavior;
4
5
use ArrayObject;
6
use Cake\Core\Configure;
7
use Cake\Event\Event;
8
use Cake\Network\Exception\InternalErrorException;
9
use Cake\ORM\Behavior;
10
use Cake\Datasource\EntityInterface;
11
use Cake\ORM\Table;
12
use Cake\ORM\TableRegistry;
13
use ContentsFile\Model\Behavior\Traits\NormalContentsFileBehaviorTrait;
14
use ContentsFile\Model\Behavior\Traits\S3ContentsFileBehaviorTrait;
15
use ContentsFile\Model\Behavior\Traits\ImageContentsFileBehaviorTrait;
16
17
class ContentsFileBehavior extends Behavior {
18
19
    use NormalContentsFileBehaviorTrait;
20
    use S3ContentsFileBehaviorTrait;
21
    use ImageContentsFileBehaviorTrait;
22
23
    /**
24
     * __construct
25
     * @author hagiwara
26
     * @param Table $table
27
     * @param array $config
28
     */
29
    public function __construct(Table $table, array $config = [])
30
    {
31
        parent::__construct($table, $config);
32
        // 指定外のものが指定されている場合はエラーとする
33
        if (!in_array(Configure::read('ContentsFile.Setting.type'), ['s3', 'normal'])) {
34
            throw new InternalErrorException('contentsFileConfig type illegal');
35
        }
36
        // Configureの設定不足をチェックする
37
        $this->{Configure::read('ContentsFile.Setting.type') . 'ParamCheck'}();
38
}
39
40
    /**
41
     * afterSave
42
     * 画像をafterSaveで保存する
43
     * @author hagiwara
44
     * @param Event $event
45
     * @param EntityInterface $entity
46
     * @param ArrayObject $options
47
     */
48
    public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        //設定値をentityから取得
51
        $contentsFileConfig = $entity->getContentsFileSettings();
0 ignored issues
show
Bug introduced by
The method getContentsFileSettings() does not seem to exist on object<Cake\Datasource\EntityInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
        $attachmentModel = TableRegistry::get('Attachments');
53
        foreach ($contentsFileConfig['fields'] as $field => $fieldSettings) {
54
            // ファイルの削除を最初に確認
55
            if ($entity->{'delete_' . $field} == true) {
56
                // 該当フィールドを削除
57
                if (!$this->fileDelete($entity, [$field])) {
58
                    return false;
59
                }
60
                // ファイルの削除に成功したら保存処理は飛ばす
61
                continue;
62
            }
63
            //contents_file_の方に入ったentityをベースに処理する
64
            $fileInfo = $entity->{'contents_file_' . $field};
65
            if (
66
                !empty($fileInfo) &&
67
                //tmp_file_nameがある=アップロードしたファイルがある
68
                array_key_exists('tmp_file_name', $fileInfo)
69
            ) {
70
                // ファイルの削除
71
                $attachmentSaveData = [
72
                    'model' => $this->_table->alias(),
73
                    'model_id' => $entity->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface Cake\Datasource\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
74
                    'field_name' => $fileInfo['field_name'],
75
                    'file_name' => $fileInfo['file_name'],
76
                    'file_content_type' => $fileInfo['file_content_type'],
77
                    'file_size' => $fileInfo['file_size'],
78
                ];
79
                $attachmentEntity = $attachmentModel->newEntity($attachmentSaveData);
80
                // 通常とS3で画像保存方法の切り替え
81
                if (!$this->{Configure::read('ContentsFile.Setting.type') . 'FileSave'}($fileInfo, $fieldSettings, $attachmentSaveData)) {
82
                    return false;
83
                }
84
85
                //元のデータがあるかfind(あれば更新にする)
86
                $attachmentDataCheck = $attachmentModel->find('all')
87
                    ->where(['model' => $fileInfo['model']])
88
                    ->where(['model_id' => $entity->id])
0 ignored issues
show
Bug introduced by
Accessing id on the interface Cake\Datasource\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
89
                    ->where(['field_name' => $fileInfo['field_name']])
90
                    ->first();
91
                if (!empty($attachmentDataCheck)) {
92
                    $attachmentEntity->id = $attachmentDataCheck->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Cake\Datasource\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
93
                }
94
                if (!$attachmentModel->save($attachmentEntity)) {
95
                    return false;
96
                }
97
            }
98
        }
99
100
        return true;
101
102
    }
103
104
    /**
105
     * fileDelete
106
     * ファイル削除
107
     * @author hagiwara
108
     * @param EntityInterface $entity
109
     * @param array $fields
110
     */
111
    public function fileDelete(EntityInterface $entity, $fields = [])
112
    {
113
        // 新規作成データ時は何もしない
114
        if (empty($entity->id)) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Cake\Datasource\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
115
            return true;
116
        }
117
        $contentsFileConfig = $entity->getContentsFileSettings();
0 ignored issues
show
Bug introduced by
The method getContentsFileSettings() does not seem to exist on object<Cake\Datasource\EntityInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
        if (!empty($contentsFileConfig['fields'])) {
119
            foreach ($contentsFileConfig['fields'] as $field => $config) {
120
                // fieldsの指定がない場合は全部消す
121
                if (!empty($fields) && !in_array($field, $fields)) {
122
                    continue;
123
                }
124
                if (!$this->fileDeleteParts($entity, $field)) {
125
                    return false;
126
                }
127
            }
128
        }
129
        return true;
130
    }
131
132
    /**
133
     * fileDeleteParts
134
     * ファイル削除
135
     * @author hagiwara
136
     * @param EntityInterface $entity
137
     * @param string $field
138
     */
139
    private function fileDeleteParts($entity, $field)
140
    {
141
        $attachmentModel = TableRegistry::get('Attachments');
142
        $modelName = $entity->source();
143
        $modelId = $entity->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Cake\Datasource\EntityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
144
        // 添付ファイルデータの削除
145
        $deleteAttachmentData = $attachmentModel->find('all')
146
            ->where(['Attachments.model' => $modelName])
147
            ->where(['Attachments.model_id' => $modelId])
148
            ->where(['Attachments.field_name' => $field])
149
            ->first();
150
151
        if (!empty($deleteAttachmentData->id)) {
152
            $attachmentModel->delete($deleteAttachmentData);
153
            // 通常とS3でファイルの削除方法の切り替え
154
            if (!$this->{Configure::read('ContentsFile.Setting.type') . 'FileDelete'}($modelName, $modelId, $field)) {
155
                return false;
156
            }
157
        }
158
        return true;
159
    }
160
161
    /**
162
     * mkdir
163
     * ディレクトリの作成(パーミッションの設定のため
164
     * @author hagiwara
165
     * @param string $permission
166
     * @param integer $path
167
     * @param boolean $recursive
168
     */
169
    private function mkdir($path, $permission, $recursive)
170
    {
171
        if (is_dir($path)) {
172
            return true;
173
        }
174
        $oldumask = umask(0);
175
        $result = mkdir($path, $permission, $recursive);
176
        umask($oldumask);
177
        return $result;
178
    }
179
}
180