MediaGalleryImage::save()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 8
nc 3
nop 3
1
<?php
2
3
namespace Magefix\Fixture\Builder\Helper;
4
5
use Magefix\Exceptions\ProductMediaGalleryImageNotFound;
6
use Magefix\Fixture\Builder\Helper;
7
8
/**
9
 * Class MediaGalleryImage
10
 * @package Magefix\Fixture\Builder\Helper
11
 * @author Carlo Tasca <[email protected]>
12
 */
13
class MediaGalleryImage implements Helper
14
{
15
    /**
16
     * @param \Mage_Core_Model_Abstract $product
17
     * @param array $gallery
18
     * @param bool $save
19
     * @throws ProductMediaGalleryImageNotFound
20
     * @throws \Exception
21
     */
22
    public static function save(\Mage_Core_Model_Abstract $product, array $gallery, $save = true)
23
    {
24
        if (!@file_exists($gallery['image'])) {
25
            throw new ProductMediaGalleryImageNotFound(
26
                'Specified product fixture gallery image does not exists -> ' . $gallery['image']
27
            );
28
        }
29
30
        $product->addImageToMediaGallery(
31
            $gallery['image'], ['image', 'thumbnail', 'small_image'], false, false
32
        );
33
34
        if ($save) {
35
            $product->save();
36
        }
37
    }
38
}
39