Proyectos de Subversion LeadersLinked - Services

Rev

Rev 383 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
307 www 1
<?php
345 www 2
declare(strict_types = 1);
307 www 3
namespace LeadersLinked\Command;
4
 
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
345 www 7
// use Symfony\Component\Console\Input\InputOption;
307 www 8
use Symfony\Component\Console\Output\OutputInterface;
9
use Laminas\Db\Adapter\AdapterInterface;
10
use Laminas\Log\LoggerInterface;
11
use LeadersLinked\Mapper\EmailMapper;
12
use PHPMailer\PHPMailer\PHPMailer;
13
use LeadersLinked\Model\Email;
14
use Laminas\Mvc\I18n\Translator;
15
use LeadersLinked\Cache\CacheInterface;
16
 
17
class ProcessScheduledContentCommand extends Command
18
{
345 www 19
 
307 www 20
    /**
21
     *
22
     * @var \Laminas\Db\Adapter\AdapterInterface
23
     */
24
    private $adapter;
345 www 25
 
307 www 26
    /**
27
     *
28
     * @var \LeadersLinked\Cache\CacheInterface
29
     */
30
    private $cache;
345 www 31
 
307 www 32
    /**
33
     *
34
     * @var \Laminas\Log\LoggerInterface
35
     */
36
    private $logger;
345 www 37
 
307 www 38
    /**
39
     *
40
     * @var array
41
     */
42
    private $config;
345 www 43
 
307 www 44
    /**
45
     *
46
     * @var \Laminas\Mvc\I18n\Translator
47
     */
48
    private $translator;
345 www 49
 
307 www 50
    /**
51
     *
52
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
53
     * @param \LeadersLinked\Cache\CacheInterface $cache
345 www 54
     * @param
55
     *            \Laminas\Log\LoggerInterface
307 www 56
     * @param array $config
57
     * @param \Laminas\Mvc\I18n\Translator $translator
58
     */
59
    public function __construct($adapter, $cache, $logger, $config, $translator)
60
    {
345 www 61
        $this->adapter = $adapter;
62
        $this->cache = $cache;
63
        $this->logger = $logger;
64
        $this->config = $config;
65
        $this->translator = $translator;
66
 
307 www 67
        parent::__construct();
68
    }
69
 
649 stevensc 70
    protected function configure(): void
71
    {
72
        $this->setName('leaderslinked:process-scheduled-content')
73
             ->setDescription('Procesa y libera el contenido programado de hábitos y habilidades')
74
             ->setHelp('Este comando verifica y procesa los hábitos programados para el día actual');
75
    }
76
 
345 www 77
    protected function execute(InputInterface $input, OutputInterface $output): int
307 www 78
    {
649 stevensc 79
        $output->writeln('Iniciando proceso de liberación de contenido programado...');
383 www 80
 
649 stevensc 81
        try {
82
            // Obtener el día de la semana actual (1 = lunes, 7 = domingo)
83
            $currentDayNumber = (int)date('N');
84
            $currentTime = date('H:i:s');
85
 
86
            // Mapear el número del día a la columna correspondiente
87
            $dayColumns = [
88
                1 => ['active' => 'monday_active', 'time' => 'monday_time'],
89
                2 => ['active' => 'tuesday_active', 'time' => 'tuesday_time'],
90
                3 => ['active' => 'wednesday_active', 'time' => 'wednesday_time'],
91
                4 => ['active' => 'thursday_active', 'time' => 'thursday_time'],
92
                5 => ['active' => 'friday_active', 'time' => 'friday_time'],
93
                6 => ['active' => 'saturday_active', 'time' => 'saturday_time'],
94
                7 => ['active' => 'sunday_active', 'time' => 'sunday_time']
95
            ];
96
 
97
            $currentDayColumns = $dayColumns[$currentDayNumber];
98
 
99
            // Obtener el mapper de hábitos
100
            $habitSkillMapper = \LeadersLinked\Mapper\HabitSkillMapper::getInstance($this->adapter);
101
 
102
            // Construir la consulta para obtener hábitos programados para hoy
103
            $select = $habitSkillMapper->getSql()->select(HabitSkillMapper::_TABLE);
104
            $select->where([
105
                $currentDayColumns['active'] => 1,
106
                $currentDayColumns['time'] . ' <= ?' => $currentTime
107
            ]);
108
 
109
            $habits = $habitSkillMapper->executeFetchAllObject($select, new \LeadersLinked\Model\HabitSkill());
110
 
111
            $processedCount = 0;
112
            foreach ($habits as $habit) {
113
                try {
114
                    // Crear un registro para el hábito
115
                    $register = new \LeadersLinked\Model\HabitSkillRegister();
116
                    $register->network_id = $habit->network_id;
117
                    $register->user_id = $habit->user_id;
118
                    $register->skill_id = $habit->id;
119
                    $register->date = date('Y-m-d');
120
                    $register->uuid = uniqid();
121
 
122
                    // Registrar el hábito
123
                    $registerMapper = \LeadersLinked\Mapper\HabitSkillRegisterMapper::getInstance($this->adapter);
124
                    if ($registerMapper->insert($register)) {
125
                        $processedCount++;
126
                        $this->logger->info(
127
                            sprintf('Hábito procesado: %s (Usuario: %d)', $habit->name, $habit->user_id),
128
                            ['habit_id' => $habit->id]
129
                        );
130
                    }
131
                } catch (\Exception $e) {
132
                    $this->logger->error(
133
                        sprintf('Error procesando hábito %d: %s', $habit->id, $e->getMessage()),
134
                        ['exception' => $e]
135
                    );
136
                }
137
            }
138
 
139
            $output->writeln(sprintf('Se procesaron %d hábitos programados', $processedCount));
140
            $output->writeln('Proceso de liberación de contenido programado completado con éxito');
141
 
142
            return Command::SUCCESS;
143
 
144
        } catch (\Exception $e) {
145
            $this->logger->error('Error en el proceso de contenido programado: ' . $e->getMessage());
146
            $output->writeln('<error>Error: ' . $e->getMessage() . '</error>');
147
 
148
            return Command::FAILURE;
149
        }
307 www 150
    }
151
}