Improved Code Sniffing (#928)
[openemr.git] / services / ProductRegistrationService.php
blobfa78fe83d2ef94c33b744c1e126a98f346db0ed4
1 <?php
2 /**
3 * ProductRegistrationService
5 * LICENSE: This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
16 * @package OpenEMR
17 * @author Matthew Vita <matthewvita48@gmail.com>
18 * @author Victor Kofia <victor.kofia@gmail.com>
19 * @link http://www.open-emr.org
22 namespace services;
24 require_once($GLOBALS['fileroot'] . "/interface/main/exceptions/invalid_email_exception.php");
25 require_once($GLOBALS['fileroot'] . "/interface/product_registration/exceptions/generic_product_registration_exception.php");
26 require_once($GLOBALS['fileroot'] . "/interface/product_registration/exceptions/duplicate_registration_exception.php");
28 class ProductRegistrationService {
29 /**
30 * Logger used primarily for logging events that are of interest to
31 * developers.
33 private $logger;
35 /**
36 * The product registration repository to be used for db CRUD operations.
38 private $repository;
40 /**
41 * Default constructor.
43 public function __construct()
45 $this->logger = new \common\logging\Logger("\services\ProductRegistrationService");
46 $database = \common\database\Connector::Instance();
47 $entityManager = $database->entityManager;
48 $this->repository = $entityManager->getRepository('\entities\ProductRegistration');
51 public function getProductStatus()
53 $this->logger->debug('Getting current product registration status');
54 $row = $this->repository->findFirst();
56 // Unboxing these here to avoid PHP 5.4 "Can't use method
57 // return value in write context" error.
58 $id = '';
59 $optOut = '';
61 if ($row !== null) {
62 $id = $row->getRegistrationId();
63 $optOut = $row->getOptOut();
66 if (empty($row)) {
67 $row = new \entities\ProductRegistration();
68 $row->setStatusAsString('UNREGISTERED');
69 } else if ($id !== 'null') {
70 $row->setStatusAsString('REGISTERED');
71 } else if (!empty($optOut) && $optOut == true) {
72 $row->setStatusAsString('OPT_OUT');
75 return $row;
78 public function registerProduct($email)
80 if (!$email || $email == 'false') {
81 $this->optOutStrategy();
82 return null;
83 } else {
84 return $this->optInStrategy($email);
88 private function optInStrategy($email)
90 $this->logger->debug('Attempting to register product with email ' . $email);
91 $curl = curl_init('https://reg.open-emr.org/api/registration');
92 curl_setopt($curl, CURLOPT_POST, true);
93 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array('email' => $email)));
94 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
95 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
96 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
97 $responseBodyRaw = curl_exec($curl);
98 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
99 curl_close($curl);
101 $this->logger->debug('Raw response from remote server: ' . $responseBodyRaw);
102 switch ($responseCode) {
103 case 201:
104 $responseBodyParsed = json_decode($responseBodyRaw);
106 $entry = new \entities\ProductRegistration();
107 $entry->setRegistrationId($responseBodyParsed->productId);
108 $entry->setEmail($email);
109 $entry->setOptOut(false);
111 $newId = $this->repository->save($entry);
112 $this->logger->debug('Successfully registered product ' . $newId);
114 return $newId;
115 break;
116 case 400:
117 throw new \InvalidEmailException($email . ' ' . xl("is not a valid email address"));
118 break;
119 case 409:
120 throw new \DuplicateRegistrationException(xl("Already registered"));
121 break;
122 default:
123 throw new \GenericProductRegistrationException(xl("Server error: try again later"));
127 // void... don't bother checking for success/failure.
128 private function optOutStrategy()
130 $this->logger->debug('Attempting to opt out of product registration');
131 $entry = new \entities\ProductRegistration();
132 $entry->setRegistrationId('null');
133 $entry->setEmail(null);
134 $entry->setOptOut(true);
136 $this->repository->save($entry);