| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace cx_newsletter;
- class cron_emails_sender
- extends service
- implements service_interface {
- public function prepare() : self {
- $this->campaigns_repository = new campaigns_repository(
- $this->get_database(),
- $this->get_tables()
- );
- $this->send_log_repository = new send_log_repository(
- $this->get_database(),
- $this->get_tables()
- );
-
- $this->customers_mapper = new customers_mapper(
- $this->get_database(),
- $this->get_tables()
- );
- $this->messages_mapper = new messages_mapper(
- $this->get_database(),
- $this->get_tables()
- );
-
- return $this;
- }
- public function process() : self {
- $next_campaign = $this->campaigns_repository->load_next(
- campaign_type::email
- );
- if ($next_campaign === null) {
- return $this;
- }
- $customers = $this->send_log_repository->load_all(
- $next_campaign,
- $this->get_settings()->get('email_count'),
- false
- );
- if (empty($customers)) {
- $next_campaign->finalize();
- $this->campaigns_repository->save($next_campaign);
- return $this->process();
- }
- foreach ($customers as $customer) {
- if ($this->send($customer, $next_campaign->message)) {
- $this->send_log_repository->mark_send($next_campaign, $customer);
- }
- }
- return $this;
- }
- private function send(customer $to, message $what) : bool {
- if (empty($to->email)) {
- return false;
- }
- if (!$what->is_complete()) {
- return false;
- }
- $mail = new single_mail();
- $mail->title = $what->title;
- $mail->content = $what->content;
- $mail->to = $to->email;
- $mail->is_html = true;
- $mailer = new mail_manager($this->get_settings());
- $mailer->prepare($mail);
-
- return $mailer->send();
- }
- private campaigns_repository $campaigns_repository;
- private send_log_repository $send_log_repository;
- private customers_mapper $customers_mapper;
- private messages_mapper $messages_mapper;
- private campaign $what;
- }
|