Model::persist()   D
last analyzed

Complexity

Conditions 28
Paths 34

Size

Total Lines 101
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 103.4684

Importance

Changes 0
Metric Value
cc 28
eloc 57
nc 34
nop 1
dl 0
loc 101
ccs 39
cts 72
cp 0.5417
crap 103.4684
rs 4.4803
c 0
b 0
f 0

How to fix   Long Method    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 Anavel\Crud\Abstractor\Eloquent;
4
5
use ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer;
6
use Anavel\Crud\Abstractor\ConfigurationReader;
7
use Anavel\Crud\Abstractor\Eloquent\Traits\HandleFiles;
8
use Anavel\Crud\Abstractor\Eloquent\Traits\ModelFields;
9
use Anavel\Crud\Abstractor\Exceptions\AbstractorException;
10
use Anavel\Crud\Contracts\Abstractor\Field as FieldContract;
11
use Anavel\Crud\Contracts\Abstractor\FieldFactory as FieldFactoryContract;
12
use Anavel\Crud\Contracts\Abstractor\Model as ModelAbstractorContract;
13
use Anavel\Crud\Contracts\Abstractor\Relation;
14
use Anavel\Crud\Contracts\Abstractor\RelationFactory as RelationFactoryContract;
15
use Anavel\Crud\Contracts\Form\Generator as FormGenerator;
16
use App;
17
use FormManager\ElementInterface;
18
use Illuminate\Database\Eloquent\Model as LaravelModel;
19
use Illuminate\Http\Request;
20
use Illuminate\Support\Collection;
21
22
class Model implements ModelAbstractorContract
23
{
24
    use ConfigurationReader;
25
    use ModelFields;
26
    use HandleFiles;
27
28
    protected $dbal;
29
    protected $relationFactory;
30
    protected $fieldFactory;
31
    protected $generator;
32
33
    protected $model;
34
    protected $config;
35
36
    protected $slug;
37
    protected $name;
38
    protected $instance;
39
40
    /**
41
     * @var bool
42
     */
43
    protected $mustDeleteFilesInFilesystem;
44
45 17
    public function __construct(
46
        $config,
47
        AbstractionLayer $dbal,
48
        RelationFactoryContract $relationFactory,
49
        FieldFactoryContract $fieldFactory,
50
        FormGenerator $generator,
51
        $mustDeleteFilesInFilesystem = false
52
    ) {
53 17
        if (is_array($config)) {
54 17
            $this->model = $config['model'];
55 17
            $this->config = $config;
56 17
        } else {
57
            $this->model = $config;
58
            $this->config = [];
59
        }
60
61 17
        $this->dbal = $dbal;
62 17
        $this->relationFactory = $relationFactory;
63 17
        $this->fieldFactory = $fieldFactory;
64 17
        $this->generator = $generator;
65 17
        $this->mustDeleteFilesInFilesystem = $mustDeleteFilesInFilesystem;
66 17
    }
67
68 3
    public function setSlug($slug)
69
    {
70 3
        $this->slug = $slug;
71
72 3
        return $this;
73
    }
74
75 2
    public function setName($name)
76
    {
77 2
        $this->name = $name;
78
79 2
        return $this;
80
    }
81
82 5
    public function setInstance($instance)
83
    {
84 5
        $this->instance = $instance;
85
86 5
        return $this;
87
    }
88
89
    public function getSlug()
90
    {
91
        return $this->slug;
92
    }
93
94
    public function getName()
95
    {
96
        return transcrud($this->name);
97
    }
98
99 1
    public function getModel()
100
    {
101 1
        return $this->model;
102
    }
103
104
    public function getInstance()
105
    {
106
        return $this->instance;
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function getConfig()
113
    {
114
        return $this->config;
115
    }
116
117
    public function isSoftDeletes()
118
    {
119
        return $this->getConfigValue('soft_deletes') ? true : false;
120
    }
121
122 8
    public function getColumns($action, $withForeignKeys = false)
123
    {
124 8
        $tableColumns = $this->dbal->getTableColumns();
125
126 8
        $filteredColumns = [];
127 8
        foreach ($tableColumns as $name => $column) {
128 8
            $filteredColumns[str_replace('`', '', $name)] = $column;
129 8
        }
130 8
        $tableColumns = $filteredColumns;
131
132 8
        $foreignKeysName = [];
133 8
        if ($withForeignKeys === false) {
134 8
            $foreignKeys = $this->dbal->getTableForeignKeys();
135
136 8
            foreach ($foreignKeys as $foreignKey) {
137
                foreach ($foreignKey->getColumns() as $columnName) {
138
                    $foreignKeysName[] = $columnName;
139
                }
140 8
            }
141 8
        }
142
143 8
        $customDisplayedColumns = $this->getConfigValue($action, 'display');
144 8
        $customHiddenColumns = $this->getConfigValue($action, 'hide') ?: [];
145
146 8
        $columns = [];
147 8
        if (!empty($customDisplayedColumns) && is_array($customDisplayedColumns)) {
148 8
            foreach ($customDisplayedColumns as $customColumn) {
149 8
                if (strpos($customColumn, '.')) {
150
                    $customColumnRelation = explode('.', $customColumn);
151
152
                    $customColumnRelationFieldName = array_pop($customColumnRelation);
153
154
                    $modelAbstractor = $this;
155
                    foreach ($customColumnRelation as $relationName) {
156
                        $nestedRelation = $this->getNestedRelation($modelAbstractor, $relationName);
157
                        $modelAbstractor = $nestedRelation->getModelAbstractor();
158
                    }
159
160
                    $relationColumns = $nestedRelation->getModelAbstractor()->getColumns($action);
0 ignored issues
show
Bug introduced by
The variable $nestedRelation does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
161
162
                    if (!array_key_exists($customColumnRelationFieldName, $relationColumns)) {
163
                        throw new AbstractorException('Column '.$customColumnRelationFieldName.' does not exist on relation '.implode('.', $customColumnRelation).' of model '.$this->getModel());
164
                    }
165
166
                    $columns[$customColumn] = $relationColumns[$customColumnRelationFieldName];
167
                } else {
168 8
                    if (!array_key_exists($customColumn, $tableColumns)) {
169
                        throw new AbstractorException('Column '.$customColumn.' does not exist on '.$this->getModel());
170
                    }
171
172 8
                    $columns[$customColumn] = $tableColumns[$customColumn];
173
                }
174 8
            }
175 8
        } else {
176
            foreach ($tableColumns as $name => $column) {
177
                if (in_array($name, $customHiddenColumns)) {
178
                    continue;
179
                }
180
181
                if (in_array($name, $foreignKeysName)) {
182
                    continue;
183
                }
184
185
                $columns[$name] = $column;
186
            }
187
        }
188
189 8
        return $columns;
190
    }
191
192
    protected function getNestedRelation(Model $modelAbstractor, $relationName)
193
    {
194
        $relations = $modelAbstractor->getRelations();
195
196
        if (!$relations->has($relationName)) {
197
            throw new AbstractorException('Relation '.$relationName.' not configured on '.$modelAbstractor->getModel());
198
        }
199
200
        $relation = $relations->get($relationName);
201
202
        if ($relation instanceof Relation) {
203
            return $relation;
204
        } else {
205
            return $relation['relation'];
206
        }
207
    }
208
209
    /**
210
     * @return \Illuminate\Support\Collection
211
     */
212 4
    public function getRelations()
213
    {
214 4
        $configRelations = $this->getConfigValue('relations');
215
216 4
        $relations = collect();
217
218 4
        if (!empty($configRelations)) {
219 4
            foreach ($configRelations as $relationName => $configRelation) {
220 4
                if (is_int($relationName)) {
221
                    $relationName = $configRelation;
222
                }
223
224 4
                $config = [];
225 4
                if ($configRelation !== $relationName) {
226 4
                    if (!is_array($configRelation)) {
227
                        $config['type'] = $configRelation;
228
                    } else {
229 4
                        $config = $configRelation;
230
                    }
231 4
                }
232
233
                /** @var Relation $relation */
234 4
                $relation = $this->relationFactory->setModel($this->instance)
235 4
                    ->setConfig($config)
236 4
                    ->get($relationName);
237
238 4
                $secondaryRelations = $relation->getSecondaryRelations();
239
240 4
                if (!$secondaryRelations->isEmpty()) {
241 1
                    $relations->put(
242 1
                        $relationName,
243 1
                        collect(['relation' => $relation, 'secondaryRelations' => $secondaryRelations])
244 1
                    );
245 1
                } else {
246 3
                    $relations->put($relationName, $relation);
247
                }
248 4
            }
249 4
        }
250
251 4
        return $relations;
252
    }
253
254
    /**
255
     * @param string|null $arrayKey
256
     *
257
     * @throws AbstractorException
258
     *
259
     * @return array
260
     */
261 2 View Code Duplication
    public function getListFields($arrayKey = 'main')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
262
    {
263 2
        $columns = $this->getColumns('list');
264
265 2
        $fieldsPresentation = $this->getConfigValue('fields_presentation') ?: [];
266
267 2
        $fields = [];
268 2
        foreach ($columns as $name => $column) {
269 2
            $presentation = null;
270 2
            if (array_key_exists($name, $fieldsPresentation)) {
271 2
                $presentation = $fieldsPresentation[$name];
272 2
            }
273
274
            $config = [
275 2
                'name'         => $name,
276 2
                'presentation' => $presentation,
277 2
                'form_type'    => null,
278 2
                'validation'   => null,
279 2
                'functions'    => null,
280 2
            ];
281
282 2
            $fields[$arrayKey][] = $this->fieldFactory
283 2
                ->setColumn($column)
284 2
                ->setConfig($config)
285 2
                ->get();
286 2
        }
287
288 2
        return $fields;
289
    }
290
291
    /**
292
     * @param string|null $arrayKey
293
     *
294
     * @throws AbstractorException
295
     *
296
     * @return array
297
     */
298 2 View Code Duplication
    public function getDetailFields($arrayKey = 'main')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
299
    {
300 2
        $columns = $this->getColumns('detail');
301
302 2
        $fieldsPresentation = $this->getConfigValue('fields_presentation') ?: [];
303
304 2
        $fields = [];
305 2
        foreach ($columns as $name => $column) {
306 2
            $presentation = null;
307 2
            if (array_key_exists($name, $fieldsPresentation)) {
308 2
                $presentation = $fieldsPresentation[$name];
309 2
            }
310
311
            $config = [
312 2
                'name'         => $name,
313 2
                'presentation' => $presentation,
314 2
                'form_type'    => null,
315 2
                'validation'   => null,
316 2
                'functions'    => null,
317 2
            ];
318
319 2
            $fields[$arrayKey][] = $this->fieldFactory
320 2
                ->setColumn($column)
321 2
                ->setConfig($config)
322 2
                ->get();
323 2
        }
324
325 2
        return $fields;
326
    }
327
328
    /**
329
     * @param bool|null   $withForeignKeys
330
     * @param string|null $arrayKey
331
     *
332
     * @throws AbstractorException
333
     *
334
     * @return array
335
     */
336 4
    public function getEditFields($withForeignKeys = false, $arrayKey = 'main')
337
    {
338 4
        $columns = $this->getColumns('edit', $withForeignKeys);
0 ignored issues
show
Bug introduced by
It seems like $withForeignKeys defined by parameter $withForeignKeys on line 336 can also be of type null; however, Anavel\Crud\Abstractor\E...ent\Model::getColumns() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
339
340 4
        $this->readConfig('edit');
341
342 4
        $fields = [];
343 4
        foreach ($columns as $name => $column) {
344 4
            if (!in_array($name, $this->getReadOnlyColumns())) {
345 4
                $presentation = null;
346 4
                if (array_key_exists($name, $this->fieldsPresentation)) {
347
                    $presentation = $this->fieldsPresentation[$name];
348
                }
349
350
                $config = [
351 4
                    'name'         => $name,
352 4
                    'presentation' => $presentation,
353 4
                    'form_type'    => null,
354 4
                    'validation'   => null,
355 4
                    'functions'    => null,
356 4
                ];
357
358 4
                $config = $this->setConfig($config, $name);
359
360 4
                $field = $this->fieldFactory
361 4
                    ->setColumn($column)
362 4
                    ->setConfig($config)
363 4
                    ->get();
364
365 4
                if (!empty($this->instance) && !empty($this->instance->getAttribute($name))) {
366
                    $field->setValue($this->instance->getAttribute($name));
367
                }
368
369 4
                $fields[$arrayKey][$name] = $field;
370
371 4 View Code Duplication
                if (!empty($config['form_type']) && $config['form_type'] === 'file') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
372 4
                    $field = $this->fieldFactory
373 4
                        ->setColumn($column)
374 4
                        ->setConfig([
375 4
                            'name'         => $name.'__delete',
376 4
                            'presentation' => null,
377 4
                            'form_type'    => 'checkbox',
378 4
                            'no_validate'  => true,
379 4
                            'functions'    => null,
380 4
                        ])
381 4
                        ->get();
382 4
                    $fields[$arrayKey][$name.'__delete'] = $field;
383 4
                }
384 4
            }
385 4
        }
386
387 4
        return $fields;
388
    }
389
390 4
    protected function getReadOnlyColumns()
391
    {
392 4
        $columns = [LaravelModel::CREATED_AT, LaravelModel::UPDATED_AT, 'deleted_at'];
393
394 4
        $columns[] = $this->dbal->getModel()->getKeyName();
395
396 4
        return $columns;
397
    }
398
399
    /**
400
     * @param string $action
401
     *
402
     * @return ElementInterface
403
     */
404 2
    public function getForm($action)
405
    {
406 2
        $this->generator->setModelFields($this->getEditFields());
407 2
        $this->generator->setRelatedModelFields($this->getRelations());
408
409 2
        return $this->generator->getForm($action);
410
    }
411
412
    /**
413
     * @param array $requestForm
0 ignored issues
show
Documentation introduced by
There is no parameter named $requestForm. Did you maybe mean $request?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
414
     *
415
     * @return mixed
416
     */
417 1
    public function persist(Request $request)
418
    {
419
        /** @var \ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager $modelManager */
420 1
        $modelManager = App::make('ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager');
421 1
        if (!empty($this->instance)) {
422
            $item = $this->instance;
423
        } else {
424 1
            $item = $modelManager->getModelInstance($this->getModel());
425
        }
426
427 1
        $fields = $this->getEditFields(true);
428
429 1
        if (empty($fields['main']) && $this->getRelations()->isEmpty()) {
430
            return;
431
        }
432
433 1
        $foreigns = $this->getEditFields(false);
434
435 1
        if (empty($fields['main'])) {
436
            $fields['main'] = array();
437
        }
438
439 1
        if (empty($foreigns['main'])) {
440
            $foreigns['main'] = array();
441
        }
442
443 1
        $foreignFields = array_diff_key($fields['main'], $foreigns['main']);
444
445 1
        if (!empty($fields['main'])) {
446 1
            $skip = null;
447 1
            foreach ($fields['main'] as $key => $field) {
448
                /* @var FieldContract $field */
449 1
                if ($skip === $key) {
450
                    $skip = null;
451
                    continue;
452
                }
453 1
                $fieldName = $field->getName();
454 1
                $requestValue = $request->input("main.{$fieldName}");
455
456 1
                if (! empty($foreignFields) && (! empty($foreignFields[$fieldName])) && (empty($requestValue))) {
457
                    $requestValue = null;
458
                }
459
460 1
                if (get_class($field->getFormField()) === \FormManager\Fields\Checkbox::class) {
461
                    if (empty($requestValue)) {
462
                        // Unchecked checkboxes are not sent, so we force setting them to false
463
                        $item->setAttribute(
464
                            $fieldName,
465
                            $field->applyFunctions(null)
466
                        );
467
                    } else {
468
                        $requestValue = true;
469
                    }
470
                }
471
472 1
                if (get_class($field->getFormField()) === \FormManager\Fields\File::class) {
473
                    $handleResult = $this->handleField($request, $item, $fields['main'], 'main', $fieldName, $this->mustDeleteFilesInFilesystem);
474
                    if (!empty($handleResult['skip'])) {
475
                        $skip = $handleResult['skip'];
476
                    }
477
                    if (!empty($handleResult['requestValue'])) {
478
                        $requestValue = $handleResult['requestValue'];
479
                    }
480
                }
481
482 1
                if (!$field->saveIfEmpty() && empty($requestValue)) {
483
                    continue;
484
                }
485
486 1
                if (! empty($requestValue)
487 1
                    || (empty($requestValue) && !empty($item->getAttribute($fieldName)))
488
                    || (! empty($foreignFields) && (! empty($foreignFields[$fieldName])) && (empty($requestValue)))
489 1
                ) {
490 1
                    $item->setAttribute(
491 1
                        $fieldName,
492 1
                        $field->applyFunctions($requestValue)
493 1
                    );
494 1
                }
495 1
            }
496 1
        }
497
498 1
        $item->save();
499
500 1
        $this->setInstance($item);
501
502 1
        if (!empty($relations = $this->getRelations())) {
503 1
            foreach ($relations as $relationKey => $relation) {
504 1
                if ($relation instanceof Collection) {
505
                    $input = $request->input($relationKey);
506
                    /** @var $relationInstance Relation */
507
                    $relationInstance = $relation->get('relation');
508
                    $relationInstance->persist($input, $request);
0 ignored issues
show
Bug introduced by
It seems like $input defined by $request->input($relationKey) on line 505 can also be of type string; however, Anavel\Crud\Contracts\Ab...tor\Relation::persist() does only seem to accept null|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
509
                } else {
510
                    /* @var $relation Relation */
511 1
                    $relation->persist($request->input($relationKey), $request);
0 ignored issues
show
Bug introduced by
It seems like $request->input($relationKey) targeting Illuminate\Http\Request::input() can also be of type string; however, Anavel\Crud\Contracts\Ab...tor\Relation::persist() does only seem to accept null|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
512
                }
513 1
            }
514 1
        }
515
516 1
        return $item;
517
    }
518
519
    /**
520
     * @return array
521
     */
522 1
    public function getValidationRules()
523
    {
524 1
        return $this->generator->getValidationRules();
525
    }
526
527
    public function getFieldValue($item, $fieldName)
528
    {
529
        $value = null;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
530
531
        if (strpos($fieldName, '.')) {
532
            $customColumnRelation = explode('.', $fieldName);
533
534
            $customColumnRelationFieldName = array_pop($customColumnRelation);
535
536
            $entity = $item;
537
            $relation = null;
538
            $relationName = '';
539
            foreach ($customColumnRelation as $relationName) {
540
                $relation = $entity->{$relationName};
541
                if ($relation instanceof Collection) {
542
                    $entity = $relation->first();
543
                } else {
544
                    $entity = $relation;
545
                }
546
            }
547
548
            if (is_null($entity)) {
549
                return;
550
            }
551
            $lastRelationName = $relationName;
552
553
            array_pop($customColumnRelation);
554
555
            $prevModelAbtractor = $this;
556
            foreach ($customColumnRelation as $relationName) {
557
                $nestedRelation = $this->getNestedRelation($prevModelAbtractor, $relationName);
558
                $prevModelAbtractor = $nestedRelation->getModelAbstractor();
559
            }
560
561
            if (empty($relation)) {
562
                return;
563
            }
564
565
            if ($relation instanceof Collection) {
566
                $relations = $prevModelAbtractor->getRelations();
567
568
                $relationAbstractor = $relations->get($lastRelationName);
569
570
                if ($relationAbstractor instanceof \Anavel\Crud\Abstractor\Eloquent\Relation\Translation) {
571
                    $value = $entity->getAttribute($customColumnRelationFieldName);
572
                } else {
573
                    $value = $relation->implode($customColumnRelationFieldName, ', ');
574
                }
575
            } else {
576
                $value = $relation->getAttribute($customColumnRelationFieldName);
577
            }
578
        } else {
579
            $value = $item->getAttribute($fieldName);
580
        }
581
582
        return $value;
583
    }
584
585
    /**
586
     * @return bool
587
     */
588
    public function mustDeleteFilesInFilesystem()
589
    {
590
        return $this->mustDeleteFilesInFilesystem;
591
    }
592
}
593