minor fix to prior commit
[openemr.git] / services / ProductRegistrationService.php
blobbab45e6002517e9df7b6b5c032dd805195c6c046
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() {
44 $this->logger = new \common\logging\Logger("\services\ProductRegistrationService");
45 $database = \common\database\Connector::Instance();
46 $entityManager = $database->entityManager;
47 $this->repository = $entityManager->getRepository('\entities\ProductRegistration');
50 public function getProductStatus() {
51 $this->logger->debug('Getting current product registration status');
52 $row = $this->repository->findFirst();
54 // Unboxing these here to avoid PHP 5.4 "Can't use method
55 // return value in write context" error.
56 $id = '';
57 $optOut = '';
59 if ($row !== null) {
60 $id = $row->getRegistrationId();
61 $optOut = $row->getOptOut();
64 if (empty($row)) {
65 $row = new \entities\ProductRegistration();
66 $row->setStatusAsString('UNREGISTERED');
67 } else if ($id !== 'null') {
68 $row->setStatusAsString('REGISTERED');
69 } else if (!empty($optOut) && $optOut == true) {
70 $row->setStatusAsString('OPT_OUT');
73 return $row;
76 public function registerProduct($email) {
77 if (!$email || $email == 'false') {
78 $this->optOutStrategy();
79 return null;
80 } else {
81 return $this->optInStrategy($email);
85 private function optInStrategy($email) {
86 $this->logger->debug('Attempting to register product with email ' . $email);
87 $curl = curl_init('https://reg.open-emr.org/api/registration');
88 curl_setopt($curl, CURLOPT_POST, true);
89 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array('email' => $email)));
90 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
91 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
92 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
93 $responseBodyRaw = curl_exec($curl);
94 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
95 curl_close($curl);
97 $this->logger->debug('Raw response from remote server: ' . $responseBodyRaw);
98 switch ($responseCode) {
99 case 201:
100 $responseBodyParsed = json_decode($responseBodyRaw);
102 $entry = new \entities\ProductRegistration();
103 $entry->setRegistrationId($responseBodyParsed->productId);
104 $entry->setEmail($email);
105 $entry->setOptOut(false);
107 $newId = $this->repository->save($entry);
108 $this->logger->debug('Successfully registered product ' . $newId);
110 return $newId;
111 break;
112 case 400:
113 throw new \InvalidEmailException($email . ' ' . xl("is not a valid email address"));
114 break;
115 case 409:
116 throw new \DuplicateRegistrationException(xl("Already registered"));
117 break;
118 default:
119 throw new \GenericProductRegistrationException(xl("Server error: try again later"));
123 // void... don't bother checking for success/failure.
124 private function optOutStrategy() {
125 $this->logger->debug('Attempting to opt out of product registration');
126 $entry = new \entities\ProductRegistration();
127 $entry->setRegistrationId('null');
128 $entry->setEmail(null);
129 $entry->setOptOut(true);
131 $this->repository->save($entry);