Minor modification ub04 support scripts.
[openemr.git] / services / ProductRegistrationService.php
blob5798e21adeef845ebacb222f18eb33a462de280a
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 OpenEMR\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
30 /**
31 * Logger used primarily for logging events that are of interest to
32 * developers.
34 private $logger;
36 /**
37 * The product registration repository to be used for db CRUD operations.
39 private $repository;
41 /**
42 * Default constructor.
44 public function __construct()
46 $this->logger = new \common\logging\Logger("\OpenEMR\Services\ProductRegistrationService");
47 $database = \common\database\Connector::Instance();
48 $entityManager = $database->entityManager;
49 $this->repository = $entityManager->getRepository('\entities\ProductRegistration');
52 public function getProductStatus()
54 $this->logger->debug('Getting current product registration status');
55 $row = $this->repository->findFirst();
57 // Unboxing these here to avoid PHP 5.4 "Can't use method
58 // return value in write context" error.
59 $id = '';
60 $optOut = '';
62 if ($row !== null) {
63 $id = $row->getRegistrationId();
64 $optOut = $row->getOptOut();
67 if (empty($row)) {
68 $row = new \entities\ProductRegistration();
69 $row->setStatusAsString('UNREGISTERED');
70 } else if ($id !== 'null') {
71 $row->setStatusAsString('REGISTERED');
72 } else if (!empty($optOut) && $optOut == true) {
73 $row->setStatusAsString('OPT_OUT');
76 return $row;
79 public function registerProduct($email)
81 if (!$email || $email == 'false') {
82 $this->optOutStrategy();
83 return null;
84 } else {
85 return $this->optInStrategy($email);
89 private function optInStrategy($email)
91 $this->logger->debug('Attempting to register product with email ' . $email);
92 $curl = curl_init('https://reg.open-emr.org/api/registration');
93 curl_setopt($curl, CURLOPT_POST, true);
94 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array('email' => $email)));
95 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
96 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
97 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
98 $responseBodyRaw = curl_exec($curl);
99 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
100 curl_close($curl);
102 $this->logger->debug('Raw response from remote server: ' . $responseBodyRaw);
103 switch ($responseCode) {
104 case 201:
105 $responseBodyParsed = json_decode($responseBodyRaw);
107 $entry = new \entities\ProductRegistration();
108 $entry->setRegistrationId($responseBodyParsed->productId);
109 $entry->setEmail($email);
110 $entry->setOptOut(false);
112 $newId = $this->repository->save($entry);
113 $this->logger->debug('Successfully registered product ' . $newId);
115 return $newId;
116 break;
117 case 400:
118 throw new \InvalidEmailException($email . ' ' . xl("is not a valid email address"));
119 break;
120 case 409:
121 throw new \DuplicateRegistrationException(xl("Already registered"));
122 break;
123 default:
124 throw new \GenericProductRegistrationException(xl("Server error: try again later"));
128 // void... don't bother checking for success/failure.
129 private function optOutStrategy()
131 $this->logger->debug('Attempting to opt out of product registration');
132 $entry = new \entities\ProductRegistration();
133 $entry->setRegistrationId('null');
134 $entry->setEmail(null);
135 $entry->setOptOut(true);
137 $this->repository->save($entry);