/var/www/html_uk/wp-content/plugins/automatewoo/includes/Workflows/Presets/PresetService.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php

namespace AutomateWoo\Workflows\Presets;

use 
AutomateWoo\Exceptions\Exception as ExceptionInterface;
use 
AutomateWoo\Logger;
use 
AutomateWoo\Workflow;
use 
AutomateWoo\Workflows\Factory;
use 
AutomateWoo\Workflows\Presets\Parser\ParserException;
use 
AutomateWoo\Workflows\Presets\Parser\PresetParserInterface;
use 
AutomateWoo\Workflows\Presets\Storage\PresetStorageInterface;
use 
AutomateWoo\Workflows\Presets\Storage\StorageException;
use 
WP_Error;

/**
 * @class PresetService
 * @since 5.1.0
 */
class PresetService {

    
/**
     * @var PresetStorageInterface
     */
    
protected $preset_storage;

    
/**
     * @var PresetParserInterface
     */
    
protected $preset_parser;

    
/**
     * PresetService constructor.
     *
     * @param PresetStorageInterface $preset_storage
     * @param PresetParserInterface  $preset_parser
     */
    
public function __constructPresetStorageInterface $preset_storagePresetParserInterface $preset_parser ) {
        
$this->preset_storage $preset_storage;
        
$this->preset_parser  $preset_parser;
    }

    
/**
     * Returns the list of available presets
     *
     * @return PresetInterface[]
     */
    
public function get_presets() {
        return 
$this->preset_storage->list();
    }

    
/**
     * Returns a preset given its ID
     *
     * @param string $id
     *
     * @return PresetInterface|WP_Error Returns the preset if found or WP_Error if it doesn't exists
     */
    
public function get_preset$id ) {
        try {
            return 
$this->preset_storage->get$id );
        } catch ( 
StorageException $e ) {
            
Logger::notice'presets'$e->getMessage() );

            
/* translators: %s: The preset ID. */
            
return new WP_Error'aw_preset_not_found'sprintf__'The preset with ID "%s" does not exist.''automatewoo' ), $id ) );
        }
    }

    
/**
     * Stores the given preset as a draft workflow
     *
     * @param PresetInterface $preset
     *
     * @return Workflow|WP_Error Returns the created workflow on success, WP_Error on failure
     */
    
public function save_as_workflowPresetInterface $preset ) {
        try {
            
$workflow $this->preset_parser->parse$preset );

            
$result Factory::create$workflow );
        } catch ( 
ParserException $e ) {
            
Logger::notice'presets'$e->getMessage() );
            
$result = new WP_Error'aw_invalid_preset'__'There were problems parsing the preset. Please check the logs for more info.''automatewoo' ) );
        } catch ( 
ExceptionInterface $e ) {
            
Logger::notice'presets'$e->getMessage() );
            
$result = new WP_Error'aw_preset_workflow'__'There were problems creating a workflow from the preset. Please check the logs for more info.''automatewoo' ) );
        } catch ( 
\Exception $e ) {
            
Logger::notice'presets'$e->getMessage() );
            
$result = new WP_Error'aw_preset_workflow'__'There were problems creating a workflow from the preset. Please check the logs for more info.''automatewoo' ) );
        }

        return 
$result;
    }

    
/**
     * Finds the preset given its ID and stores it as a draft workflow
     *
     * @param string $preset_id
     *
     * @return Workflow|WP_Error Returns the created workflow on success, WP_Error on failure
     */
    
public function save_as_workflow_by_id$preset_id ) {
        try {
            
$preset $this->preset_storage->get$preset_id );
        } catch ( 
ExceptionInterface $e ) {
            
Logger::notice'presets'$e->getMessage() );
            return new 
WP_Error'aw_preset_get'__'There were problems retrieving the preset. Please check the logs for more info.''automatewoo' ) );
        }

        return 
$this->save_as_workflow$preset );
    }
}