Completed
Pull Request — develop (#6)
by Carlo
02:28
created

MediaGalleryImage::save()   A

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